• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
August 18, 2022 |1.3K Views
How to Initialize a List in Java
  Share   Like
Description
Discussion

In this video, we will see how to initialize a List in Java.

The discussed methods are as follows:
1. Using .add() Method of List
2. Using .asList() Method.
3. Using collections
4. Using Stream

Method 1: In this method we can use, list.add() to add a single element to the list.

Method 2: In this method we can use, Arrays.asList() to create an immutable list  of elements.
To make this list mutable, we have to pass the result of Arrays.asList() to new ArrayList instance.

Method 3: In this method we can use, ArrayList() to create a new list. We can add elements to this list using Collections.addAll() method and passing the list reference variable followed by the elements to enter.
We can also make this list Immutable, by using Collections.unmodifiable() method and passing the Array.asList() output in the parameters.

Method 4: In this method, we can use, java.utils.stream.Stream to create a list from a Stream.

Initializing a List in Java
https://www.geeksforgeeks.org/initializing-a-list-in-java/