import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
public class UnionToHashSet {
public static void main(String[] args) {
// TODO Auto-generated method stub
Collection collections = java.util.Arrays.asList("asdf","java");
System.out.println(unionToHashSet(collections));
}
public static <T> HashSet<T> unionToHashSet(
final Collection<T>... collections) {
final HashSet<T> res = new HashSet<T>();
for (final Collection<T> set : collections) {
res.addAll(set);
}
return res;
}
public static <T> HashSet<T> unionToHashSet(
final Collection<T> collection, final T... items) {
final HashSet<T> result = new HashSet<T>();
result.addAll(collection);
Collections.addAll(result, items);
return result;
}
}
Console:
[java, asdf]