209

I want to convert String array to ArrayList. For example String array is like:

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

How to convert this String array to ArrayList?

3
  • 21
    +1; it's now the second link on google. Commented Mar 22, 2013 at 0:32
  • 3
    ArrayList<String> a = new ArrayList<>(Arrays.asList(words)); Commented Mar 16, 2017 at 15:07
  • You can do it in 3 basic ways. Using Collections.addAll, Arrays.asList and streams in Java 8 - Reference - codingeek.com/java/how-to-convert-array-to-list-in-java Commented Jul 28, 2017 at 6:24

5 Answers 5

340

Use this code for that,

import java.util.Arrays;  
import java.util.List;  
import java.util.ArrayList;  

public class StringArrayTest {

   public static void main(String[] args) {  
      String[] words = {"ace", "boom", "crew", "dog", "eon"};  

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

      for (String e : wordList) {  
         System.out.println(e);  
      }  
   }  
}
Sign up to request clarification or add additional context in comments.

11 Comments

In summary all you need is the line List<String> wordList = Arrays.asList(words); . Is this correct?
@Keale If words is an array of strings, then, yes.
Beware: Arrays.asList(...) creates an AbstractList, not a real list. So you can do stuff like wordList.sort((a, b) -> a.length() - b.length()), but you can not do wordList.remove(0), or add to it. This will throw UnsupportedOperationException.
This code creates a List but it is not an ArrayList. So it is an incorrect answer. You cant delete elements from that List -> will throw an exception.
@Vito I have to agree that this is probably not a solution to a real problem unless someone can tell my why you would want to convert String[] to List and then not update the list.
|
184
new ArrayList( Arrays.asList( new String[]{"abc", "def"} ) );

4 Comments

i think that this solution is better because Arrays.asList return a java.util.Arrays.ArrayList.ArrayList<T>(T[]) so if you try to add something you'll get a java.lang.UnsupportedOperationException
If you have limited number of elemenets in the array, it can be written as new ArrayList( Arrays.asList("abc", "def"));
To avoid IDE warnings, new ArrayList<>(...) worked for me as opposed to new ArrayList(...) (note the <>).
Best answer for me. With List<String> wordList = Arrays.asList(words);, doing wordList instanceof ArrayList<String> will return false.
55

Using Collections#addAll()

String[] words = {"ace","boom","crew","dog","eon"};
List<String> arrayList = new ArrayList<>(); 
Collections.addAll(arrayList, words); 

4 Comments

I like this solution. It is concise and clean.
Quite why this is not the accepted answer is unknown to me.
This is the right answer. asList is wrong. use Collection.addAll(arrayList, string array)...
This is the right solution if you want an mutable list from an array which is likely what we are looking for anyway. In the off chance an immutable list is needed, the above code accomplishes that task.
30
String[] words= new String[]{"ace","boom","crew","dog","eon"};
List<String> wordList = Arrays.asList(words);

2 Comments

The result here is a List, but not an ArrayList.
@MarkoTopolnik you are right, this needs to be wrapped so List<String> wordList = new ArrayList<String>(Arrays.asList(words));
12

in most cases the List<String> should be enough. No need to create an ArrayList

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

...

String[] words={"ace","boom","crew","dog","eon"};
List<String> l = Arrays.<String>asList(words);

// if List<String> isnt specific enough:
ArrayList<String> al = new ArrayList<String>(l);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.