259

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'];
    }
}
0

6 Answers 6

493

In Java 9+ you can use List.of to conveniently produce an unmodifiable list.

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

Java 8 using Stream:

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

Or, in Java 16+, more briefly done with toList:

Stream.of("xyz", "abc").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:

Constructs an empty list with an initial capacity of ten.

Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

Constructs an empty list with the specified initial capacity.

Sign up to request clarification or add additional context in comments.

7 Comments

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.
Why you wrap by new ArrayList<>()? List<String> x = Arrays.asList("xyz", "abc") is ok
No harm in repeating parts of the doc - StackOverflow is probably more visited anyway.
@mystdeim Because Arrays.asList returns an unmodifiable list, meaning trying to add/remove an element it will throw an UnsupportedOperationException.
|
42

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

1 Comment

Thanks for the fixed size. It is faster on contains method of Set<List<T>> = Arrays.asList("a","b");
32

You can do like this :

List<String> temp = new ArrayList<String>(Arrays.asList("1", "12"));

Comments

11

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.

Comments

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

Comments

7

Use this one:

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

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.