Java Collection Intersect intersection(Collection> sets)

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

Description

Determines the intersection of a collection of sets.

License

Open Source License

Parameter

Parameter Description
T a parameter
sets Basic collection of sets.

Return

The set of common elements of all given sets.

Declaration

public static <T> Set<T> intersection(Collection<Set<T>> sets) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;
import java.util.Collection;

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

import java.util.Set;

public class Main {
    /**/*w ww  .ja va 2  s .c  om*/
     * Determines the intersection of a collection of sets.
     * 
     * @param <T>
     * @param sets
     *            Basic collection of sets.
     * @return The set of common elements of all given sets.
     */
    public static <T> Set<T> intersection(Collection<Set<T>> sets) {
        Set<T> result = new HashSet<>();
        if (sets.isEmpty()) {
            return result;
        }
        Iterator<Set<T>> iter = sets.iterator();
        result.addAll(iter.next());
        while (iter.hasNext()) {
            result.retainAll(iter.next());
        }
        return result;
    }

    /**
     * Determines the intersection of a collection of sets.
     * 
     * @param <T>
     * @param sets
     *            Basic collection of sets.
     * @return The set of common elements of all given sets.
     */
    public static <T> Set<T> intersection(Set<T>... sets) {
        return intersection(Arrays.asList(sets));
    }
}

Related

  1. intersection( Collection> coll)
  2. intersection(Collection a, Collection b)
  3. intersection(Collection> collectionOfCollections)
  4. intersection(Collection> availableValuesByDescriptor)
  5. intersection(Collection... collections)
  6. intersection(Collection a, Collection b)
  7. intersection(Collection a, Collection b)
  8. intersection(Collection a, Collection b)
  9. intersection(Collection values1, Collection values2)