Here you can find the source of union(final Iterable
public static <T> Set<T> union(final Iterable<Set<T>> elements)
//package com.java2s; /**/*from w w w .j a v a2 s. c o m*/ * Copyright (C) 2013 * by 52 North Initiative for Geospatial Open Source Software GmbH * * Contact: Andreas Wytzisk * 52 North Initiative for Geospatial Open Source Software GmbH * Martin-Luther-King-Weg 24 * 48155 Muenster, Germany * info@52north.org * * This program is free software; you can redistribute and/or modify it under * the terms of the GNU General Public License version 2 as published by the * Free Software Foundation. * * This program is distributed WITHOUT ANY WARRANTY; even without the implied * WARRANTY OF MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program (see gnu-gpl v2.txt). If not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA or * visit the Free Software Foundation web page, http://www.fsf.org. */ import java.util.Collections; import java.util.HashSet; import java.util.Set; public class Main { public static <T> Set<T> union(final Set<T>... elements) { return ((elements.length == 0) ? Collections.<T> emptySet() : new HashSet<T>(elements.length * elements[0].size()) { private static final long serialVersionUID = -3161916411604210423L; { for (final Set<T> s : elements) { addAll(s); } } }); } public static <T> Set<T> union(final Iterable<Set<T>> elements) { return new HashSet<T>() { private static final long serialVersionUID = -3161916411604210423L; { for (final Set<T> s : elements) { addAll(s); } } }; } }