Creates a new Set with the inferred type using the given elements. - Java java.util

Java examples for java.util:Set Creation

Description

Creates a new Set with the inferred type using the given elements.

Demo Code


//package com.book2s;

import java.util.Collection;

import java.util.HashSet;

import java.util.Set;

public class Main {
    /**//from w  ww  . j a v  a2s  . com
     * Creates a new {@link Set} with the inferred type
     * using the given elements.
     */
    public static <T> Set<T> set(T... vals) {
        HashSet<T> ret = new HashSet<T>();
        for (T v : vals)
            ret.add(v);
        return ret;
    }

    /**
     * Creates a new {@link Set} with the inferred type
     * using the given elements.
     */
    public static <T> Set<T> set(Collection<T> vals) {
        return new HashSet<T>(vals);
    }
}

Related Tutorials