Java Utililty Methods Collection Intersect

List of utility methods to do Collection Intersect

Description

The list of methods to do Collection Intersect are organized into topic(s).

Method

Setintersect(Collection... collections)
Returns the intersection set of a series of input collections.
if (collections.length == 0)
    return new LinkedHashSet<A>();
Set<A> intersectionSet = new LinkedHashSet<A>();
intersectionSet.addAll(collections[0]);
for (int i = 1; i < collections.length; i++) {
    intersectionSet.retainAll(collections[i]);
return intersectionSet;
...
Listintersect(Collection a, Collection b)
Returns a copy of a containing only elements also in b .
List<T> result = new ArrayList<>();
for (T t : a) {
    if (b.contains(t)) {
        result.add(t);
return Collections.unmodifiableList(result);
Collectionintersect(Collection collA, Collection collB, Collection target)
return a new Collection with the elements which appear both in a and b.
Complexity: for sets - linear, for lists - quadratic.
target.clear();
for (T elem : collA) {
    if (collB.contains(elem)) {
        target.add(elem);
return target;
Collectionintersect(Collection collection1, Collection collection2)
Given two collections of elements of type , return a collection with the items which only appear in both lists
if (isEmpty(collection1) || isEmpty(collection2)) {
    return Collections.emptyList();
Set<T> intersection = new HashSet<T>(collection1);
intersection.retainAll(collection2);
return intersection;
booleanintersect(Collection t1, Collection t2)
intersect
for (T t : t1) {
    if (t2.contains(t)) {
        return true;
return false;
CollectionintersectCollections(Collection collection1, Collection collection2)
Returns all items that are both in collection1 and in collection2
Collection<T> result = new ArrayList<T>(collection1);
result.retainAll(collection2);
return result;
booleanintersectingCollections(Collection collection1, Collection collection2)
intersecting Collections
if (collection1 == null || collection2 == null) {
    return false;
int size1 = collection1.size();
int size2 = collection2.size();
if (size1 < size2) {
    for (Object obj : collection1)
        if (collection2.contains(obj)) {
...
CintersectInto(C into, Collection... from)
intersect Into
if (from.length > 0) {
    into.addAll(from[0]);
    for (int i = 1; i < from.length; i++) {
        into.retainAll(from[i]);
return into;
Setintersection(Collection> coll)
Returns the intersection of all the collections given in a collection.
Iterator<? extends Collection<? extends T>> i = coll.iterator();
if (!i.hasNext())
    return new HashSet<T>();
Set<T> set = new HashSet<T>(i.next());
while (i.hasNext()) {
    Collection<? extends T> innerColl = i.next();
    set.retainAll(innerColl);
return set;
Collectionintersection(Collection a, Collection b)
intersection
Collection results = new HashSet();
for (Iterator i = a.iterator(); i.hasNext();) {
    Object o = i.next();
    if (b.contains(o)) {
        results.add(o);
return results;
...