2

How to Initialize a List in Java

 2 years ago
source link: https://www.geeksforgeeks.org/videos/how-to-initialize-a-list-in-java/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to Initialize a List in Java

How to Initialize a List in Java
Welcome to the Expo Geeks. My name is Ian and today
  • 18/08/2022

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/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK