union sets - Java Collection Framework

Java examples for Collection Framework:Set

Description

union sets

Demo Code


//package com.java2s;

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;
                    {/* w  ww.  ja  v  a 2  s  .c  o  m*/
                        for (final Set<T> s : elements) {
                            addAll(s);
                        }
                    }
                });
    }
}

Related Tutorials