Java Collection Intersect intersection( Collection> coll)

Here you can find the source of intersection( Collection> coll)

Description

Returns the intersection of all the collections given in a collection.

License

Open Source License

Parameter

Parameter Description
coll A Collection of sets

Declaration

public static <T> Set<T> intersection(
        Collection<? extends Collection<? extends T>> coll) 

Method Source Code

//package com.java2s;
// Clark & Parsia, LLC parts of this source code are available under the terms of the Affero General Public License v3.

import java.util.Collection;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Main {
    /**/* w w  w . j a va 2 s .  c  o m*/
     * Returns the intersection of all the collections given in a collection. 
     * 
     * @param coll A Collection of sets
     */
    public static <T> Set<T> intersection(
            Collection<? extends Collection<? extends T>> coll) {
        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;
    }

    /**
     * Returns the intersection of two collections 
     * 
     * @param coll A Collection of sets
     */
    public static <T> Set<T> intersection(Collection<? extends T> c1,
            Collection<? extends T> c2) {
        Set<T> set = new HashSet<T>(c1);
        set.retainAll(c2);

        return set;
    }
}

Related

  1. intersect(Collection collection1, Collection collection2)
  2. intersect(Collection t1, Collection t2)
  3. intersectCollections(Collection collection1, Collection collection2)
  4. intersectingCollections(Collection collection1, Collection collection2)
  5. intersectInto(C into, Collection... from)
  6. intersection(Collection a, Collection b)
  7. intersection(Collection> collectionOfCollections)
  8. intersection(Collection> availableValuesByDescriptor)
  9. intersection(Collection... collections)