Java tutorial
//package com.java2s; import java.lang.reflect.Array; import java.util.Collection; import java.util.HashSet; public class Main { @SafeVarargs public static <T> HashSet<T> newHashSet(T... ts) { HashSet<T> set = new HashSet<T>(); for (T t : ts) { set.add(t); } return set; } public static <T> HashSet<T> newHashSet(Collection<T> collection) { HashSet<T> set = new HashSet<T>(); set.addAll(collection); return set; } @SafeVarargs public static <T> T[] addAll(T[]... arrays) { if (arrays.length == 1) { return arrays[0]; } int length = 0; for (T[] array : arrays) { if (array == null) { continue; } length += array.length; } T[] result = newArray(arrays.getClass().getComponentType().getComponentType(), length); length = 0; for (T[] array : arrays) { if (array == null) { continue; } System.arraycopy(array, 0, result, length, array.length); length += array.length; } return result; } @SuppressWarnings("unchecked") public static <T> T[] newArray(Class<?> componentType, int newSize) { return (T[]) Array.newInstance(componentType, newSize); } }