Java Set Union union(Set... sets)

Here you can find the source of union(Set... sets)

Description

Returns the union of multiple sets.

License

Open Source License

Parameter

Parameter Description
sets Multiple sets which will be united.
T Type of values.

Return

A set forming the union of the given sets.

Declaration

@SafeVarargs
public static <T> Set<T> union(Set<T>... sets) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    /**/* ww  w  .j  a v  a2  s  .  c  o  m*/
     * Returns the union of multiple sets.
     * <p>
     * The original sets are not altered.
     *
     * @param sets Multiple sets which will be united.
     * @param <T>  Type of values.
     * @return A set forming the union of the given sets.
     */
    @SafeVarargs
    public static <T> Set<T> union(Set<T>... sets) {
        if (sets == null)
            return new HashSet<>();
        Set<T> result = new HashSet<>();
        for (Set<T> s : sets) {
            if (s != null)
                result.addAll(s);
        }
        return result;
    }
}

Related

  1. union(Set one, Set two)
  2. union(Set a, Set b)
  3. union(Set left, Set right)
  4. union(Set setA, Set setB)
  5. union(Set setA, Set setB)
  6. union(String[] set1, String[] set2)