In this post, I’d like to share with you some ways you can use to create a List collection (usually an ArrayList) with some initial values in just a single line. Basically you may create an ArrayList object and then add some elements as shown in the following example:

List<String> listFruits = new ArrayList<>();

listFruits.add("Apple");
listFruits.add("Lemon");
listFruits.add("Banana");
listFruits.add("Guava");
listFruits.add("Mango");
However, this code is somewhat quite long and boilerplate. Are there any more compact ways? The answer is yes, fortunately.

 

1. Using Arrays.asList() method

You can initialize a List with some fixed values in one line using the Arrays.asList() method. The above code can be rewritten in more compact way as shown below (import the Arrays class from java.util package):

List<String> listFruits = Arrays.asList("Apple", "Lemon", "Banana", "Guava", "Mango");
This looks easier and more convenient, right? But what is the difference?

Well, the Arrays.asList() method returns a fixed-size list, which means you cannot add or remove elements to or from the returned collection. Any attempts to add or remove elements will cause runtime error:

// will throw UnsupportedOperationException:
listFruits.add("Bananas");
listFruits.remove("Lemon");
So, what if you want to initialize a List with some values in a single line, and later you can add/remove elements if needed? Then you should construct an ArrayList like this:

List<String> listFruits = new ArrayList<>(Arrays.asList("Apple", "Lemon", "Banana", "Guava", "Mango"));
You know, this line creates an ArrayList object with initial elements taken from the Arrays.asList() method. The created list is actually an ArrayList so you can add or remove elements later.

Let’s see more examples. The following line creates a fixed-size list of integer numbers:

List<Number> listNumbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);


And to make it a sizable ArrayList:

List<Number> listNumbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 0));
Given the User class defined as follows:

public class User {
	String name;
	
	public User(String name) {
		this.name = name;
	}
}
Then the following line of code initializes a List with 3 User objects:

List<User> listUsers = Arrays.asList(new User("Tom"), new User("John"), new User("Alex"));
So note that the Arrays.asList() method returns a fixed-size list. If you want to get a sizable collection, construct an ArrayList object that wraps the list returned by Arrays.asList() method.

 

2. Using List.of() method

Since Java 9, you can use the List.of() method to initialize a List with some initial values in a single line. Let’s see the following example:

List<String> listAnimals = List.of("Dog", "Cat", "Mouse", "Cow", "Duck");
This line creates an unmodifiable list containing the given elements. That means you cannot add/remove elements to the list after initialization. If you attempt to do so, you will get UnsupportedOperationException thrown at runtime.

So if you want to have a modifiable list, wrap the return of List.of() method inside constructor of the ArrayList class, as shown in the following example:

List<String> listAnimals = new ArrayList<>(List.of("Dog", "Cat", "Mouse", "Cow", "Duck"));
Similarly, the following line of code initializes a list of numbers with some fixed values:

List<Integer> primeNumbers = List.of(2, 3, 5, 7, 11, 13, 17, 19);
And the following example initializes an ArrayList with some values of type User:

List<User> listUsers = new ArrayList<>(List.of(new User("Alice"), new User("Edward"), new User("Peter")));
So far I have shared with you some different ways to create a List collection initialized with some fixed elements using either Arrays.asList() or List.of() method. It’s important to note that both methods return an unmodifiable list, so if you want to have modifiable one you must construct an ArrayList class as shown in the code examples above.

 

References:

 

Other Java Collections Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment