union two Sets - Java java.util

Java examples for java.util:Set Union

Description

union two 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;
                    {//from www  . j a v  a 2  s  .com
                        for (final Set<T> s : elements) {
                            addAll(s);
                        }
                    }
                });
    }
}

Related Tutorials