Initialize List with Values in Java Initialize List with Values in Java

Page content

In this tutorial, we’ll learn different ways to initialize List, ArrayList and LinkedList with values in single line in Java.

Java 8 or earlier

Initialize Immutable List

This is the simplest way to initialize a List:-

/**
* Immutable list with inline elements
*/
List<String> list = Arrays.asList("foo", "bar", "baz");

/**
* Immutable list with array
*/
String[] names = { "foo", "bar" };
List<String> anotherList = Arrays.asList(names);

anotherList.add("baz") // Throw UnsupportedOperationException exception

The only drawback is that the initalized list is immutable. That means adding or removing elements in the list throw java.lang.UnsupportedOperationException exception.

It is useful when you just need it for iteration and read-only purpose.


Initialize Mutable List

If you want to intialize a mutable list where you can add or remove elements. You wrap immutable list with ArrayList or LinkedList:-

List<String> arrayList = new ArrayList<>(Arrays.asList("foo", "bar"));
arrayList.add("baz"); // It works!


List<String> linkedList = new LinkedList<>(Arrays.asList("foo", "bar"));
linkedList.remove("foo"); // It works!

Collections.addAll()

You can also use Collections.addAll() static method to add values to ArrayList or LinkedList

List<String> arrayList = new ArrayList<String>();
Collections.addAll(arrayList, "foo", "bar", "baz");

Double Brace Initialization

Another way is to making an anonymous inner class with an instance initializer. This is also known as an double brace initialization. However that looks like an overkill to create a inner class just to create an ArrayList or LinkedList

List<String> strings = new ArrayList<String>() {
  {
    add("A");
    add("B");
    add("C");
  }
};

Java 9 or later

Initialize Immutable List

List.of() is added in Java 9 which can be used to initialize an immutable list with values.

List<String> list = List.of("foo", "bar");
list.add("baz") // Throw UnsupportedOperationException exception

With Java 10 or later, this can be even more shortened with the var keyword.

var list = List.of("foo", "bar", "baz");

Initialize Mutable List

You can also define mutable list with ArrayList or LinkedList wrapper:-

List<String> arrayList = new ArrayList<>(List.of("foo", "bar"));
arrayList.add("baz"); // It works!

List<String> linkedList = new LinkedList<>(List.of("foo", "bar"));
linkedList.remove("foo"); // It works!

Using Streams

You can also use Stream API which is more flexible:-

Stream<String> strings = Stream.of("foo", "bar", "baz");

You can combine two or more Streams like this:-

Stream<String> strings = Stream.concat(Stream.of("foo", "bar"), Stream.of("baz", "qux"));

You can transform a Stream into mutable List or ArrayList like this:-

List<String> list = Stream.of("foo", "bar", "baz").collect(Collectors.toList());
list.remove("baz"); // It works!

List<String> arrayList = Stream.of("foo", "bar").collect(Collectors.toCollection(ArrayList::new));
arrayList.add("baz"); // It works!