How to convert String[] (Array) to Collection, like ArrayList or HashSet?
So how to convert String array to String in java. We can use Arrays. toString method that invoke the toString() method on individual elements and use StringBuilder to create String. We can also create our own method to convert String array to String if we have some specific format requirements.
We can simply convert a Set into a List using the constructor of an ArrayList or LinkedList.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With