244

ArrayList or List declaration in Java has questioned and answered how to declare an empty ArrayList but how do I declare an ArrayList with values?

I've tried the following but it returns a syntax error:

import java.io.IOException;
import java.util.ArrayList;

public class test {
    public static void main(String[] args) throws IOException {
        ArrayList<String> x = new ArrayList<String>();
        x = ['xyz', 'abc'];
    }
}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
alvas
  • 115,346
  • 109
  • 446
  • 738

6 Answers6

469

In Java 9+ you can do:

var x = List.of("xyz", "abc");
// 'var' works only for local variables

Java 8 using Stream:

Stream.of("xyz", "abc").collect(Collectors.toList());

And of course, you can create a new object using the constructor that accepts a Collection:

List<String> x = new ArrayList<>(Arrays.asList("xyz", "abc"));

Tip: The docs contains very useful information that usually contains the answer you're looking for. For example, here are the constructors of the ArrayList class:

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • 3
    Use List on the LHS rather than ArrayList, if you don't absolutly need an ArrayList there. And use the diamond operator on the RHS to avoid warnings. – Puce Feb 11 '14 at 08:50
  • 5
    Why you wrap by new ArrayList<>()? `List x = Arrays.asList("xyz", "abc")` is ok – mystdeim Nov 30 '15 at 07:21
  • 6
    No harm in repeating parts of the doc - StackOverflow is probably more visited anyway. – Graham Apr 02 '16 at 11:05
  • `List.of()`is Java 9, not 10+: https://docs.oracle.com/javase/9/docs/api/java/util/List.html – Det Sep 21 '19 at 07:12
  • 2
    @mystdeim Because Arrays.asList returns an unmodifiable list, meaning trying to add/remove an element it will throw an UnsupportedOperationException. – seBaka28 Oct 05 '20 at 14:51
  • Minor side note: the first example using List.of() returns an immutable list, and not specifically an ArrayList as the question requested. – DanJ Jan 06 '21 at 01:28
40

Use:

List<String> x = new ArrayList<>(Arrays.asList("xyz", "abc"));

If you don't want to add new elements to the list later, you can also use (Arrays.asList returns a fixed-size list):

List<String> x = Arrays.asList("xyz", "abc");

Note: you can also use a static import if you like, then it looks like this:

import static java.util.Arrays.asList;

...

List<String> x = new ArrayList<>(asList("xyz", "abc"));

or

List<String> x = asList("xyz", "abc");
Puce
  • 37,247
  • 13
  • 80
  • 152
31

You can do like this :

List<String> temp = new ArrayList<String>(Arrays.asList("1", "12"));
Vimal Bera
  • 10,346
  • 4
  • 25
  • 47
11

The Guava library contains convenience methods for creating lists and other collections which makes this much prettier than using the standard library classes.

Example:

ArrayList<String> list = newArrayList("a", "b", "c");

(This assumes import static com.google.common.collect.Lists.newArrayList;)

Lii
  • 11,553
  • 8
  • 64
  • 88
10

Try this!

List<String> x = new ArrayList<String>(Arrays.asList("xyz", "abc"));

It's a good practice to declare the ArrayList with interface List if you don't have to invoke the specific methods.

Drogba
  • 4,186
  • 4
  • 23
  • 31
6

Use this one:

ArrayList<String> x = new ArrayList(Arrays.asList("abc", "mno"));
Rajendra arora
  • 2,186
  • 1
  • 16
  • 23