How to Initialize ArrayList in Java

Hassan Saeed Feb 02, 2024
  1. Use Arrays.asList to Initialize an ArrayList in Java
  2. Use new ArrayList() Initialize an ArrayList in Java
  3. Use List.of() to Initialize an ArrayList in Java
  4. Use Stream to Initialize an ArrayList in Java
How to Initialize ArrayList in Java

This tutorial discusses methods to initialize an ArrayList with values in one line in Java. It is handy for testing and minimalistic coding.

Use Arrays.asList to Initialize an ArrayList in Java

It is relatively easier to initialize a list instead of an ArrayList in Java with initial values in one line. However, if needed, it can be converted to an ArrayList. The below example illustrates both ways.

import java.util.*;

public class Main {
  public static void main(String[] args) {
    // Initialize a list
    List<String> cities = Arrays.asList("Amsterdam", "Paris", "London");

    // Initialize an ArrayList
    ArrayList<String> cities_AL = new ArrayList<>(Arrays.asList("Amsterdam", "Paris", "London"));

    System.out.println(cities);
    System.out.println(cities_AL);
  }
}

Output:

[Amsterdam, Paris, London]
[Amsterdam, Paris, London]

The only difference between the cities and cities_AL is that one is a List and the other is an ArrayList.

Use new ArrayList() Initialize an ArrayList in Java

Another method to initialize an ArrayList is using the new ArrayList() function. You can pre-assign all the values in a single line of code.

import java.util.*;

public class Main {
  public static void main(String[] args) {
    // Pre assign the values
    ArrayList<String> cities = new ArrayList<String>() {
      {
        add("Amsterdam");
        add("Paris");
        add("London");
      }
    };

    System.out.println(cities);
  }
}

Output:

[Amsterdam, Paris, London]

Use List.of() to Initialize an ArrayList in Java

In Java9 or later, we can use the List.of() method instead of Arrays.asList() to initialize an ArrayList in Java. The below example illustrates this.

import java.util.*;

public class Main {
  public static void main(String[] args) {
    // Initialize a List
    List<String> strings = List.of("foo", "bar", "baz");
    System.out.println(strings);

    // Initialize an ArrayList
    ArrayList<String> strings_a = new ArrayList<>(List.of("foo", "bar", "baz"));
    System.out.println(strings_a);
  }
}

Output:

[foo, bar, baz]
[foo, bar, baz]

Use Stream to Initialize an ArrayList in Java

We can also use Stream to initialize an ArrayList in Java. The below example illustrates this.

import java.util.*;
import java.util.stream.*;
public class Main {
  public static void main(String[] args) {
    ArrayList<String> places = new ArrayList<>(
        Stream.of("Buenos Aires", "Córdoba", "La Plata").collect(Collectors.toList()));

    System.out.println(places);
  }
}

Output:

[Buenos Aires, Córdoba, La Plata]

We have discussed several ways to initialize an ArrayList in Java in just one line with pre-assigning values in this article.

Related Article - Java ArrayList