190

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?

Samuel Liew
  • 68,352
  • 105
  • 140
  • 225
user1386570
  • 1,933
  • 2
  • 12
  • 4

5 Answers5

315

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);  
      }  
   }  
}
Marcel Bro
  • 4,356
  • 3
  • 37
  • 62
Lalit Bhudiya
  • 3,988
  • 4
  • 24
  • 32
  • 13
    In summary all you need is the line `List wordList = Arrays.asList(words); `. Is this correct? – Keale Aug 05 '14 at 02:40
  • 1
    @Keale If `words` is an array of strings, then, yes. – Nicolai S Oct 22 '15 at 22:06
  • 22
    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. – Nicolai S Oct 22 '15 at 22:08
  • 9
    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. – Witold Kaczurba Mar 16 '17 at 15:04
  • @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. – Jeff Holt Jul 07 '17 at 19:26
  • @jeff6times7 You can use Collections API on it, lambdas, filter it, display, concatenate, sort, whatever... etc. Loads of things – Witold Kaczurba Jul 08 '17 at 00:52
  • @Vito I got confused by the trailing sentence in your comment. If you had not written about changes to the list, then I would have had no need to ask you for clarification. When you said you can't delete from it, I went down the rabbit trail of thinking that you rejected the answer because you can't delete from it. But deleting from it is irrelevant because it's not a correct answer. It's that simple. – Jeff Holt Jul 08 '17 at 01:12
  • The question was to convert to ArrayList. It does not produce that class type. It produces object that implements List but not ArrayList. The For uses of the former or latter refer to documentation or other forums. – Witold Kaczurba Jul 08 '17 at 06:00
  • and remember we can't alter wordList. wordList.add("you can't") will hit you on your head with java.lang.UnsupportedOperationException. – pavan Aug 16 '17 at 06:50
  • This answer does not create an "ArrayList" as asked by the OP... – angryITguy Mar 13 '20 at 05:42
173
new ArrayList( Arrays.asList( new String[]{"abc", "def"} ) );
Matten
  • 16,627
  • 1
  • 38
  • 61
  • 4
    i think that this solution is better because `Arrays.asList` return a `java.util.Arrays.ArrayList.ArrayList(T[])` so if you try to `add` something you'll get a `java.lang.UnsupportedOperationException` – Manuel Spigolon Feb 19 '15 at 15:39
  • 1
    If you have limited number of elemenets in the array, it can be written as new ArrayList( Arrays.asList("abc", "def")); – Diptopol Dam Mar 30 '15 at 03:46
  • 1
    To avoid IDE warnings, `new ArrayList<>(...)` worked for me as opposed to `new ArrayList(...)` (note the `<>`). – Farbod Salamat-Zadeh Aug 26 '15 at 14:31
  • 2
    Best answer for me. With `List wordList = Arrays.asList(words);`, doing `wordList instanceof ArrayList` will return false. – alvgarvilla May 24 '16 at 15:39
52

Using Collections#addAll()

String[] words = {"ace","boom","crew","dog","eon"};
List<String> arrayList = new ArrayList<>(); 
Collections.addAll(arrayList, words); 
isvforall
  • 7,860
  • 6
  • 27
  • 46
27
String[] words= new String[]{"ace","boom","crew","dog","eon"};
List<String> wordList = Arrays.asList(words);
Scorpion
  • 3,882
  • 21
  • 37
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);
helt
  • 3,563
  • 3
  • 26
  • 42