Java Create ArrayList with values [4 ways]

In this post, I will be sharing how to create an ArrayList with values in Java. There are 4 ways to achieve our goal:

1. Using the Java9 Factory method [Easiest but immutable]

2. Using Java8 Stream

3. Using the Arrays.asList() method

4. Using double braces [Not Recommended]

Read Also: How to Initialize an ArrayList in Java

Java Create ArrayList with Values

1. Using the Java9 (or Later) factory method


In a single line statement, we can easily create an ArrayList with values by using Java9(or later) List.of() static factory methods. According to Oracle docs, the List.of() static factory methods provide a convenient way to create immutable lists.

 import java.util.List;
public class CreateArrayListWithValues {
    public static void main(String args[]) {
        List<String> list = List.of("Laptop","Desktop","Tablet");
        for (String str : list)
        {
            System.out.println(str);
        }
    }
}


Output:
Laptop
Desktop
Tablet

2. Using Java8 Stream


We can also use of() method of Stream to initialize an ArrayList in Java as shown below in the example. We can add or remove elements from the list with this approach.

 import java.util.List;
import java.util.stream.Stream;
import java.util.stream.Collectors;

public class CreateArrayListWithValues2 {
    public static void main(String args[]) {
        List<String> list = Stream.of("Laptop","Desktop","Tablet").collect(Collectors.toList());
        for (String str : list)
        {
            System.out.println(str);
        }
    }
}


Output:
Laptop
Desktop
Tablet

3. Using Arrays.asList() method


3.1 For String Values


Another way to create ArrayList with values is by using the Arrays.asList() method and passing it to ArrayList's constructor as shown below in the example.

 import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;

public class CreateArrayListWithValues3 {
    public static void main(String args[]) {
        List<String> list = new ArrayList(Arrays.asList("Laptop","Desktop","Tablet"));
        for (String str : list)
        {
            System.out.println(str);
        }
    }
}


Output:
Laptop
Desktop
Tablet

3.2 For Integer Values


 ArrayList<Integer> list = new ArrayList(Arrays.asList(8,4,19,89));


3.3 For Float Values


 ArrayList<Float> list = new ArrayList(Arrays.asList(8.0f,4.3f,19.2f,89.1f));


4. Using double braces


There is another way to create ArrayList with values i.e. by using double braces. This approach is not recommended because it creates an anonymous class internally that takes to verbose code and complexity.

 import java.util.ArrayList;

public class CreateArrayListWithValues4 {
    public static void main(String args[]) {
        ArrayList<String> list = new ArrayList(){{add("Laptop");add("Desktop");add("Tablet");}};
        for (String str : list)
        {
            System.out.println(str);
        }
    }
}


Output:
Laptop
Desktop
Tablet

That's all for today. Please mention in the comments if you have any questions related to how to create ArrayList with values in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry