Here you can find the source of intersection(Collection
Parameter | Description |
---|---|
T | a parameter |
sets | Basic collection of sets. |
public static <T> Set<T> intersection(Collection<Set<T>> sets)
//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)); } }