Create an Object Set from the supplied objects. - Java java.util

Java examples for java.util:Set Creation

Description

Create an Object Set from the supplied objects.

Demo Code


//package com.java2s;
import java.util.*;

public class Main {
    /**//from   w w  w .  j  a  v  a  2  s  .c  om
     * Create an Object {@link Set} from the supplied objects.
     * @param objects The objects to be added to the set.
     * @return The {@link Set}.
     */
    public static <T> Set<T> toSet(T... objects) {
        Set<T> theSet = new HashSet<T>();
        addToCollection(theSet, objects);
        return theSet;
    }

    private static <T> void addToCollection(Collection<T> theCollection,
            T... objects) {
        for (T object : objects) {
            theCollection.add(object);
        }
    }
}

Related Tutorials