Alex Rivera | Logout

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

Asked 2012-08-16T11:58:31.857
84

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

Edit
Report

2 Answers

166

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)); 
answered 2012-08-16T12:02:02.627
2
java.util.Arrays.asList(new String[]{"a", "b"})
answered 2012-08-16T12:00:08.883

Your Answer