Java tutorial
//package com.java2s; import java.util.Collection; import java.util.HashSet; import java.util.Map; import java.util.Set; public class Main { public static <T> Set<T> union(Set<T> set1, Set<T> set2) { if (isEmpty(set1)) { return set2; } if (isEmpty(set2)) { return set1; } Set<T> result = createHashSet(set1); result.addAll(set2); return result; } public static boolean isEmpty(Map<?, ?> map) { return (map == null) || (map.size() == 0); } public static boolean isEmpty(Collection<?> collection) { return (collection == null) || (collection.size() == 0); } public static <E> HashSet<E> createHashSet() { return new HashSet<E>(); } public static <E> HashSet<E> createHashSet(int initialCapacity) { return new HashSet<E>(initialCapacity); } public static <E> HashSet<E> createHashSet(Collection<? extends E> collection) { if (collection == null) { return new HashSet<E>(); } return new HashSet<E>(collection); } public static <E, O extends E> HashSet<E> createHashSet(O... args) { if (args == null || args.length == 0) { return new HashSet<E>(); } HashSet<E> set = new HashSet<E>(args.length); for (O o : args) { set.add(o); } return set; } public static <T> HashSet<T> createHashSet(Iterable<? extends T> iter) { HashSet<T> set; if (iter instanceof Collection<?>) { set = new HashSet<T>((Collection<? extends T>) iter); } else { set = new HashSet<T>(); iterableToCollection(iter, set); } return set; } private static <E> void iterableToCollection(Iterable<? extends E> iter, Collection<E> list) { if (iter == null) { return; } for (E element : iter) { list.add(element); } } }