Java tutorial
//package com.java2s; import java.util.Collections; import java.util.HashSet; import java.util.Set; public class Main { /** * Creates an unmodifiable set filled with the given values. * * @param <T> * the set's element type. * @param values * the values. * @return a newly created set containing the given values. */ @SafeVarargs public static <T> Set<T> createUnmodifiableSet(final T... values) { switch (values.length) { case 0: return Collections.emptySet(); case 1: return Collections.singleton(values[0]); default: return Collections.unmodifiableSet(createHashSet(values)); } } /** * Creates a <code>HashSet</code> filled with the given values. * * @param <T> * the set's element type. * @param values * the values. * @return a newly created <code>HashSet</code> containing the given values. */ @SafeVarargs public static <T> Set<T> createHashSet(final T... values) { final Set<T> set = new HashSet<T>(values.length); return addToSet(set, values); } /** * Adds several values to a set. * * @param <T> * the set's element type. * @param set * the set. * @param values * the values to be added. * @return the given set. */ @SafeVarargs public static <T> Set<T> addToSet(final Set<T> set, final T... values) { for (final T t : values) { set.add(t); } return set; } }