Here you can find the source of union(Collection
Parameter | Description |
---|---|
T | a parameter |
sets | Basic collection of sets. |
public static <T> Set<T> union(Collection<Set<T>> sets)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; public class Main { /**//www . j a v a2 s . com * Determines the union of a collection of sets. * * @param <T> * @param sets * Basic collection of sets. * @return The set of distinct elements of all given sets. */ public static <T> Set<T> union(Collection<Set<T>> sets) { Set<T> result = new HashSet<>(); if (sets.isEmpty()) { return result; } Iterator<Set<T>> iter = sets.iterator(); result.addAll(iter.next()); if (sets.size() == 1) { return result; } while (iter.hasNext()) { result.addAll(iter.next()); } return result; } public static <T> Set<T> union(Set<T>... sets) { List<Set<T>> list = new ArrayList<>(sets.length); for (Set<T> set : sets) { list.add(set); } return union(list); } }