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

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


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)); 


The easiest way would be:

String[] myArray = ...;List<String> strs = Arrays.asList(myArray);

using the handy Arrays utility class. Note, that you can even do

List<String> strs = Arrays.asList("a", "b", "c");


Collections.addAll provides the shortest (one-line) receipt

Having

String[] array = {"foo", "bar", "baz"}; Set<String> set = new HashSet<>();

You can do as below

Collections.addAll(set, array);