Last Updated on 18 July 2024   |   Print Email
In this post, I’d like to share with you some ways you can use to create a Listcollection (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):
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:
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:
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:
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.To see the coding in action, watch the following video:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments