Write code to set Union - Java java.util

Java examples for java.util:Set Union

Requirements

Write code to set Union

Demo Code

//package com.java2s;

import java.util.Collections;

import java.util.HashSet;

import java.util.Set;

public class Main {
    @SafeVarargs//w w w . j a  va 2  s  . c om
    public static <A> Set<A> setUnion(Set<A>... sets) {
        if (sets.length == 0)
            return Collections.<A> emptySet();
        else if (sets.length == 1)
            return sets[0];
        else {
            Set<A> set = new HashSet<A>();
            for (Set<A> xs : sets)
                set.addAll(xs);
            return set;
        }
    }
}

Related Tutorials