Javatpoint Logo
Javatpoint Logo

Initializing a List in Java

One of Collection's derived interfaces is Java.util.List. It is an ordered group of objects that allows for the storing of duplicate values. List enables positional access and element insertion since it maintains the insertion order. The Vector, Stack, LinkedList, and ArrayList classes to implement the List Interface.

Initializing a list in Java is a crucial step in the development process as it defines the initial state of the list and prepares it for further operations. There are various ways to initialize a list in Java, and the choice depends on the specific requirements of the project.

List is an interface, and the following methods can be used to create instances of List:

The following are the different approaches to initialize list.

  1. Using List.add() method
  2. Using Arrays.asList()
  3. Using Collections class methods
  4. Using Java 8 Stream
  5. Using Java 9 List.of()

Approach: Using List.add() method

List cannot be instantiated directly because it is an interface. On the other hand, objects of the classes that have defined this interface can be created and instantiated.

Add(): The add() method is a commonly used method in Java that is used to add elements to a collection or list. This method is available for several types of collections in Java, including List, Set, and Map.

A few examples of classes that have implemented the List interface include Vector, LinkedList, Stack, and ArrayList.

Syntax:

There are two cases in this approach.

  1. General method
  2. Double brace initialization

Case 1: General method

Implementation:

FileName: GeneralMethod.java

Output:

The ArrayList is : [10, 30]
The LinkedList is : [20, 40]
The Stack is : [30, 10]

Case 2: Double Brace Initialization

Implementation:

FileName: Doublebrace.java

Output:

The ArrayList is : [10, 30]
The LinkedList is : [20, 40]
The Stack is : [30, 10]

Approach: Using Arrays.asList()

There are mainly two cases present in this approach.

  1. By creating an Immutable List
  2. By creating a Mutable List

Case 1: By creating an Immutable List

We can use Arrays.asList() to convert an array into an immutable list. So, it is possible to utilise an array to instantiate a list.

Implementation:

FileName: ImmutableList.java

Output:

The List is : [10, 20, 30]
The List is : [60, 20, 50]

Case 2: By creating a Mutable List

Syntax:

List<Integer> list_1=new ArrayList<>(Arrays.asList(element 1, element 2, element 3));

Implementation:

FileName: Mutablelist.java

Output:

The first created List : [10, 20, 30]
The Modified list is given by : [10, 20, 30, 50, 40]

Approach: Using Collections class methods

A list can be instantiated using a variety of methods in the Collections class. They are as follows:

  1. By using Collections.addAll()
  2. By using Collections.unmodifiableList()
  3. By using Collections.singletonList()

Case 1: Using Collections.addAll()

The static function addAll() in the Collections class can be used to initialise a list. Any amount of elements can be entered into Collections.addAll() once the Collection in which the elements are to be inserted is defined.

Syntax:

Implementation:

FileName: CollectionsAddAll.java

Output:

The List is given by : [10, 20, 30, 40]

Case 2: By using Collections.unmodifiableList()

The list returned by Collections.unmodifiableList() cannot be changed; that is, no element can be added or removed. An UnsupportedOperationExample will be generated if there is any attempt to alter the list.

Syntax:

Implementation:

FileName: UnmodifableList.java

Output:

The List is given by : [10, 20, 30]

Case 3: By using Collections.singletonList()

Collections.singletonList() returns back an unchangeable list with just one element.

Syntax:

Implementation:

FileName: SingletonList.java

Output:

The List is given by : [10]

Approach: Using Java 8 Stream

As of Java 8, one can create any stream of objects and then compile them into a list by using the addition of Stream and functional programming.

The Stream interface offers the toList() method, which is an abstraction to collect the Stream elements in the form of an immutable Java List. The toList() method returns a new List containing all Stream elements. Alternatively, we can use toCollection() method of the Collectors class to provide a List instance. The toCollection() collects all Stream elements into the given List and returns it.

Syntax:

Implementation:

FileName: java8Stream.java

Output:

The List using Syntax 1 is given by : [10, 20, 30, 40]
The List using Syntax 2 is given by : [10, 20, 30, 40]
The List using Syntax 3 is given by : [10, 20, 30, 40]

Approach: Using Java 9 List.of()

The introduction of Java 9 The List.of() function creates a reduced to, immutable list from any number of arguments it receives.

Java 9 introduced a factory method in the List class that returns an instance of an immutable ArrayList containing the given element. This is the most straightforward way of creating and initiating an ArrayList in the same line.

Syntax:

Implementation:

FileName: Java9Example.java

Output:

The List is given by : [10, 20, 30, 40]






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA