854
Set<T> mySet = new HashSet<T>();
Collections.addAll(mySet, myArray);
That's Collections.addAll(java.util.Collection, T...) from JDK 6.
Additionally: what if our array is full of primitives?
For JDK < 8, I would just write the obvious for loop to do the wrap and add-to-set in one pass.
For JDK >= 8, an attractive option is something like:
Arrays.stream(intArray).boxed().collect(Collectors.toSet());
After you do Arrays.asList(array) you can execute Set set = new HashSet(list);
Here is a sample method, you can write:
public <T> Set<T> GetSetFromArray(T[] array) {
return new HashSet<T>(Arrays.asList(array));
}
new HashSet<Object>(Arrays.asList(Object[] a));
But I think this would be more efficient:
final Set s = new HashSet<Object>();
for (Object o : a) { s.add(o); }