A Java List can be initialized in various ways. However, initializing a list with multiple elements can be a tedious task. This tutorial will discuss different techniques to initialize a list in one line to make your work easy and reduce boilerplate code.
1. Using List.of() [Java 9]
Java 9 provides factory methods like List.Of() which makes it convenient to initialize a list with a number of elements. Note that the list instances created would be unmodifiable.
List<String> unmodifiableList = List.of("Value1", "Value2", "Value3");
2. Using Arrays.asList() to Convert Array to List
To initialize a list in a single line statement, we can get all elements in the form of an array and create a list from them using Arrays.asList() method.
List<String> MyList = Arrays.asList("Value1", "Value2", "Value3");
3. Collecting Stream Items to List
Java Stream provides some factory methods like toList() & toCollection() to collect Stream elements into a list conveniently. We need to iterate over the Stream elements and collect the entries into a new list using collectors.
The toList() method returns the collector interface that gathers the input data onto a new list.
List<String> MyList = Stream.of("Value1", "Value2", "Value3").collect(Collectors.toList());
The toCollection() method is used to create a Collection using Collector. It returns a Collector that gathers the input data into a new Collection as shown in the below example.
List<String> MyList = Stream.of("Value1", "Value2", "Value3").collect(Collectors.toCollection(ArrayList::new));
4. Creating an Unmodifiable List
4.1. Using Collections
The collection class provides various methods that can operate on a collection to return a list of elements. One such method is Collections.unmodifiableList(), which returns an unmodifiable view of a specified list. The method takes a list as a parameter for which an unmodifiable view is expected.
List<String> MyList = Collections.unmodifiableList(Arrays.asList("Value1", "Value2", "Value3"));
If you are looking for a list with one element then we can use Collections.singletonList(). It returns an unmodifiable list containing only one element.
List<String> MyList = Collections.singletonList("Value1");
4.2. Using Guava
We will use the Guava library to initialize a list in this approach. Guava provides various methods to create immutable list instances. In the below example, ImmutableList.of() method returns an immutable list containing the specified elements
List<String> MyList = ImmutableList.of("Value1", "Value2", "Value3");
One more such method is ImmutableList.copyOf() which also returns an immutable list containing the elements of the specified list. Note that it returns a NullPointerException if any of the elements is null.
ImmutableList<String> MyList = ImmutableList.copyOf(Arrays.asList("Value1", "Value2", "Value3"));
5. Conclusion
This Java tutorial taught us to initialize a list in one line using different techniques from Streams to Guava. We also created unmodifiable lists. If your need is to create an unmodifiable list then list initialization using collections would be a better approach; otherwise, we can go with Streams or by using arrays.
Happy Learning !!
Leave a Reply