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

JavaArraysCollectionsConverter

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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

Solution 3 - Java

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

Solution 4 - Java

It's a old code, anyway, try it:

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class StringArrayTest
{
   public static void main(String[] args)
   {
      String[] words = {"word1", "word2", "word3", "word4", "word5"};
 
      List<String> wordList = Arrays.asList(words);

      for (String e : wordList)
      {
         System.out.println(e);
      }
    }
}

Solution 5 - Java

If you really want to use a set:

String[] strArray = {"foo", "foo", "bar"};  
Set<String> mySet = new HashSet<String>(Arrays.asList(strArray));
System.out.println(mySet);

output:

[foo, bar]

Solution 6 - Java

Whilst this isn't strictly an answer to this question I think it's useful.

Arrays and Collections can bother be converted to Iterable which can avoid the need for performing a hard conversion.

For instance I wrote this to join lists/arrays of stuff into a string with a seperator

public static <T> String join(Iterable<T> collection, String delimiter) {
    Iterator<T> iterator = collection.iterator();
    if (!iterator.hasNext())
        return "";

    StringBuilder builder = new StringBuilder();

    T thisVal = iterator.next();
    builder.append(thisVal == null? "": thisVal.toString());

    while (iterator.hasNext()) {
        thisVal = iterator.next();
        builder.append(delimiter);
        builder.append(thisVal == null? "": thisVal.toString());
    }

    return builder.toString();
}

Using iterable means you can either feed in an ArrayList or similar aswell as using it with a String... parameter without having to convert either.

Solution 7 - Java

java.util.Arrays.asList(new String[]{"a", "b"})

Solution 8 - Java

The easiest way is through

Arrays.asList(stringArray);

Solution 9 - Java

String[] w = {"a", "b", "c", "d", "e"};  

List<String> wL = Arrays.asList(w);  

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarkView Question on Stackoverflow
Solution 1 - JavaMohanView Answer on Stackoverflow
Solution 2 - JavaDirkView Answer on Stackoverflow
Solution 3 - Javadax-nbView Answer on Stackoverflow
Solution 4 - JavaAdelmo PereiraView Answer on Stackoverflow
Solution 5 - JavaReimeusView Answer on Stackoverflow
Solution 6 - JavaJonnyRaaView Answer on Stackoverflow
Solution 7 - JavaPhilippe MarschallView Answer on Stackoverflow
Solution 8 - JavaKeppilView Answer on Stackoverflow
Solution 9 - JavagksView Answer on Stackoverflow