Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.efeichong.generator;
import java.util.*;
/**
* @author lxk
* @date 2020/10/21
* @description 集合处理工具类
*/
class CollectionUtils {
/**
* An empty unmodifiable collection.
* The JDK provides empty Set and List implementations which could be used for
* this purpose. However they could be cast to Set or List which might be
* undesirable. This implementation only implements Collection.
*/
@SuppressWarnings("rawtypes") // we deliberately use the raw type here
public static final Collection EMPTY_COLLECTION = Collections.emptyList();
/**
* com.efeichong.util.CollectionUtils should not normally be instantiated.
*/
private CollectionUtils() {
}
/**
* Returns the immutable EMPTY_COLLECTION with generic type safety.
*
* @param the element type
* @return immutable empty collection
* @see #EMPTY_COLLECTION
* @since 4.0
*/
@SuppressWarnings("unchecked") // OK, empty collection is compatible with any type
public static Collection emptyCollection() {
return EMPTY_COLLECTION;
}
/**
* Returns an immutable empty collection if the argument is null,
* or the argument itself otherwise.
*
* @param the element type
* @param collection the collection, possibly null
* @return an empty collection if the argument is null
*/
public static Collection emptyIfNull(final Collection collection) {
return collection == null ? CollectionUtils.emptyCollection() : collection;
}
/**
* Returns a {@link Collection} containing the union of the given
* {@link Iterable}s.
*
* The cardinality of each element in the returned {@link Collection} will
* be equal to the maximum of the cardinality of that element in the two
* given {@link Iterable}s.
*
* @param a the first collection, must not be null
* @param b the second collection, must not be null
* @param the generic type that is able to represent the types contained
* in both input collections.
* @return the union of the two collections
* @see Collection#addAll
*/
public static Collection union(final Iterable extends O> a, final Iterable extends O> b) {
final SetOperationCardinalityHelper helper = new SetOperationCardinalityHelper<>(a, b);
for (final O obj : helper) {
helper.setCardinality(obj, helper.max(obj));
}
return helper.list();
}
/**
* Returns a {@link Collection} containing the intersection of the given
* {@link Iterable}s.
*
* The cardinality of each element in the returned {@link Collection} will
* be equal to the minimum of the cardinality of that element in the two
* given {@link Iterable}s.
*
* @param a the first collection, must not be null
* @param b the second collection, must not be null
* @param the generic type that is able to represent the types contained
* in both input collections.
* @return the intersection of the two collections
* @see Collection#retainAll
* @see #containsAny
*/
public static Collection intersection(final Iterable extends O> a, final Iterable extends O> b) {
final SetOperationCardinalityHelper helper = new SetOperationCardinalityHelper<>(a, b);
for (final O obj : helper) {
helper.setCardinality(obj, helper.min(obj));
}
return helper.list();
}
/**
* Returns a {@link Collection} containing the exclusive disjunction
* (symmetric difference) of the given {@link Iterable}s.
*
* The cardinality of each element e in the returned
* {@link Collection} will be equal to
* max(cardinality(e,a),cardinality(e,b)) - min(cardinality(e,a),
* cardinality(e,b)).
*
* This is equivalent to
* {@code ({@link #union union(a,b)},{@link #intersection intersection(a,b)})}
* or
* {@code {@link #union union}({@link #subtract subtract(a,b)},{@link #subtract subtract(b,a)})}.
*
* @param a the first collection, must not be null
* @param b the second collection, must not be null
* @param the generic type that is able to represent the types contained
* in both input collections.
* @return the symmetric difference of the two collections
*/
public static Collection disjunction(final Iterable extends O> a, final Iterable extends O> b) {
final SetOperationCardinalityHelper helper = new SetOperationCardinalityHelper<>(a, b);
for (final O obj : helper) {
helper.setCardinality(obj, helper.max(obj) - helper.min(obj));
}
return helper.list();
}
/**
* Returns true iff all elements of {@code coll2} are also contained
* in {@code coll1}. The cardinality of values in {@code coll2} is not taken into account,
* which is the same behavior as {@link Collection#containsAll(Collection)}.
*
* In other words, this method returns true iff the
* {@link #intersection} of coll1 and coll2 has the same cardinality as
* the set of unique values from {@code coll2}. In case {@code coll2} is empty, {@code true}
* will be returned.
*
* This method is intended as a replacement for {@link Collection#containsAll(Collection)}
* with a guaranteed runtime complexity of {@code O(n + m)}. Depending on the type of
* {@link Collection} provided, this method will be much faster than calling
* {@link Collection#containsAll(Collection)} instead, though this will come at the
* cost of an additional space complexity O(n).
*
* @param coll1 the first collection, must not be null
* @param coll2 the second collection, must not be null
* @return true iff the intersection of the collections has the same cardinality
* as the set of unique elements from the second collection
* @since 4.0
*/
public static boolean containsAll(final Collection> coll1, final Collection> coll2) {
if (coll2.isEmpty()) {
return true;
}
final Iterator> it = coll1.iterator();
final Set