Wednesday, September 26, 2012

Convert Java array to a Set easily

How to convert an array to a java.util.Set easily

Have you ever wondered why there is no Arrays.asSet()? Do you loop over your array to add each element to a Set to make it a set? Well there is a much easier way. Its also why there is no Arrays.asSet().
String[] strArray = new String[] {"a", "b", "c"};
...
Set arrayAsSet = new HashSet(Arrays.asList(strArray));
Simple as that.

No comments:

Post a Comment