Creates a Set with the given elements. - Java java.util

Java examples for java.util:Set Creation

Description

Creates a Set with the given elements.

Demo Code


//package com.java2s;
import java.util.LinkedHashSet;
import java.util.Set;
import static java.util.Collections.addAll;

public class Main {
    /**/*  w  w  w.  jav  a2s .c  o m*/
     * Creates a {@link Set} with the given elements.
     *
     * @param elements the elements.
     * @param <T>      the element type.
     * @return a new {@link Set} instance containing the given elements.
     * @throws NullPointerException if parameter elements is {@code null}.
     */
    public static <T> Set<T> asSet(T... elements) {
        final Set<T> resultSet = new LinkedHashSet<T>();
        addAll(resultSet, elements);

        return resultSet;
    }
}

Related Tutorials