Java: How to convert String[] to List or Set

Mark picture Mark · Aug 16, 2012 · Viewed 173.4k times · Source

How to convert String[] (Array) to Collection, like ArrayList or HashSet?

Answer

Mohan picture Mohan · Aug 16, 2012

Arrays.asList() would do the trick here.

String[] words = {"ace", "boom", "crew", "dog", "eon"};   

List<String> wordList = Arrays.asList(words);  

For converting to Set, you can do as below

Set<T> mySet = new HashSet<T>(Arrays.asList(words));