Initializing a List in Java
The Java.util.List is a child interface of Collection. It is an ordered collection of objects where duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by the ArrayList, LinkedList, Vector, and Stack classes.
Example: Initializing and using a List to store Integer elements in Java.
// Initializing and using a List in Java
// To store integer elements
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
class Geeks
{
public static void main(String[] args)
{
// Initializing a List
List<Integer> l;
// Creating a List using the ArrayList class
l = new ArrayList<>();
// Adding elements to the List
l.add(3);
l.add(5);
l.add(7);
// Displaying the List
System.out.println("List: " + l);
}
}
Output
List: [3, 5, 7]
Table of Content
Core Interfaces in Collections
This is the visual representation of core interfafces of List in collections. The List interface is a part of the java.util package.

Different Instances of List
List is an interface, and the instances of List can be created in the following ways:
List a = new ArrayList();
List b = new LinkedList();
List c = new Vector();
List d = new Stack();
Example: Creating ArrayList, LinkedList, Vector and Stack using the List Interface.
import java.util.*;
import java.util.function.Supplier;
public class Geeks
{
public static void main(String args[])
{
// For ArrayList
List<Integer> l = new ArrayList<Integer>();
l.add(1);
l.add(3);
System.out.println("ArrayList : " + l.toString());
// For LinkedList
List<Integer> ll = new LinkedList<Integer>();
ll.add(2);
ll.add(4);
System.out.println("LinkedList : " + ll.toString());
// For Stack
List<Integer> stack = new Stack<Integer>();
stack.add(3);
stack.add(1);
System.out.println("Stack : " + stack.toString());
}
}
Output
ArrayList : [1, 3] LinkedList : [2, 4] Stack : [3, 1]
1. Double Braces Initialization
We can also initialize the list using the double braces.
Syntax:
List<Integer> list=new ArrayList<Integer>(){{
add(1);
add(2);
add(3);
}};
Example: Creating a list using the double braces initialization.
import java.util.*;
public class Geeks
{
public static void main(String args[])
{
// For ArrayList
List<Integer> l = new ArrayList<Integer>() {{
add(1);
add(3);
} };
System.out.println("ArrayList : " + l.toString());
// For LinkedList
List<Integer> ll = new LinkedList<Integer>() {{
add(2);
add(4);
} };
System.out.println("LinkedList : " + ll.toString());
// For Stack
List<Integer> stack = new Stack<Integer>() {{
add(3);
add(1);
} };
System.out.println("Stack : " + stack.toString());
}
}
Output
ArrayList : [1, 3] LinkedList : [2, 4] Stack : [3, 1]
2. Using Arrays.asList()
Creating Immutable ListArrays.asList() which creates an immutable list from an array. Hence it can be used to instantiate a list with an array.
Syntax:
List<Integer> list=Arrays.asList(1, 2, 3);
Example: Creating an Immutable list using the method Arrays.asList().
import java.util.Arrays;
import java.util.List;
public class Geeks
{
public static void main(String args[])
{
// Instantiating List using Arrays.asList()
List<Integer> l = Arrays.asList(1, 2, 3);
// Print the list
System.out.println("List : " + l.toString());
}
}
Output
List : [1, 2, 3]
Creating Mutable List:
If we want to create the mutable List. We can use the syntax mentioned below using Arrays.asList() method.
Syntax:
List<Integer> list=new ArrayList<>(Arrays.asList(1, 2, 3));
Example: Creating mutable List using the method Arrays.asList() method.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Geeks
{
public static void main(String args[])
{
// Creating a mutable list using Arrays.asList()
List<Integer> list = new ArrayList<>(
Arrays.asList(1, 2, 3));
// Print the list
System.out.println("List : " + list.toString());
list.add(5);
// Print the list
System.out.println("Modified list : " + list.toString());
}
}
Output
List : [1, 2, 3] Modified list : [1, 2, 3, 5]
3. Using Collections Class Methods
There are various methods in Collections class that can be used to instantiate a list. They are:
Using Collections.addAll():
Collections class has a static method addAll() which can be used to initialize a list. Collections.addAll() take in any number of elements after it is specified with the Collection in which the elements are to be inserted.
Syntax:
List<Integer> list = Collections.EMPTY_LIST;
Collections.addAll(list = new ArrayList<Integer>(), 1, 2, 3, 4)
Example: Using Collections.addAll method to initialize a list.
import java.util.*;
public class Geeks
{
public static void main(String args[])
{
// Create an empty list
List<Integer> list = new ArrayList<Integer>();
// Instantiating list using Collections.addAll()
Collections.addAll(list, 1, 2, 3, 4);
// Print the list
System.out.println("List : " + list.toString());
}
}
Output
List : [1, 2, 3, 4]
Using Collections.unmodifiableList()
Collections.unmodifiableList() returns a list which can’t be altered i.e. it can neither add or delete an element. Any attempt to modify the list will result in an UnsupportedOperationExample.
Syntax:
List<Integer> list = Collections
.unmodifiableList(Arrays.asList(1, 2, 3));
Example 1: Creating a list which can not be modified using the method Collections.unmodifiable.List()
import java.util.*;
public class Geeks
{
public static void main(String args[])
{
// Creating the list
List<Integer> list = Collections.unmodifiableList(
Arrays.asList(1, 2, 3));
// Print the list
System.out.println("List : " + list.toString());
}
}
Output
List : [1, 2, 3]
Example 2: Trying to modify the Collections.unmodifiableList().
import java.util.*;
public class Geeks
{
public static void main(String args[])
{
try {
// Creating the list
List<Integer> list = Collections.unmodifiableList(
Arrays.asList(1, 2, 3));
// Print the list
System.out.println("List : " + list.toString());
// Trying to modify the list
System.out.println("Trying to modify the list");
list.set(0, list.get(0));
}
catch (Exception e) {
System.out.println("Exception : " + e);
}
}
}
Output
List : [1, 2, 3] Trying to modify the list Exception : java.lang.UnsupportedOperationException
Using Collections.singletonList()
Collections.singletonList() returns an immutable list consisting of one element only.
Syntax:
List<Integer> list = Collections.singletonList(2);
Example : Creating a singleton list(Constains only single element) using the method Collections.singletonsList()
import java.util.*;
public class Geeks
{
public static void main(String args[])
{
// Creating the list
List<Integer> list = Collections.singletonList(2);
// Print the list
System.out.println("List : " + list.toString());
}
}
Output
List : [2]
4. Using Java 8 Stream
With the introduction of Stream and functional programming in Java 8, now one can construct any stream of objects and then collect them as a list.
Syntax:
1. List<Integer> list
= Stream.of(1, 2, 3)
.collect(Collectors.toList());
2. List<Integer> list
= Stream.of(1, 2, 3)
.collect(Collectors.toCollection(ArrayList::new));
3. List<Integer> list
= Stream.of(1, 2, 3, 4)
.collect(Collectors.collectingAndThen(Collectors.toList(),
Collections::unmodifiableList));
Example: Creating a list using the Java Streams.
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Geeks
{
public static void main(String args[])
{
// Creating a List using Syntax 1
List<Integer> l1 = Stream.of(1, 2, 3)
.collect(Collectors.toList());
// Printing the list
System.out.println("List using Syntax 1: "
+ l1.toString());
// Creating a List using Syntax 2
List<Integer> l2 = Stream
.of(3, 2, 1)
.collect(
Collectors
.toCollection(ArrayList::new));
// Printing the list
System.out.println("List using Syntax 2: "
+ l2.toString());
// Creating a List using Syntax 3
List<Integer> l3 = Stream
.of(1, 2, 3, 4)
.collect(
Collectors
.collectingAndThen(
Collectors.toList(),
Collections::unmodifiableList));
// Printing the list
System.out.println("List using Syntax 3: "
+ l3.toString());
}
}
Output
List using Syntax 1: [1, 2, 3] List using Syntax 2: [3, 2, 1] List using Syntax 3: [1, 2, 3, 4]
5. Using Java 9 List.of()
Java 9 introduced List.of() method which takes in any number of arguments and constructs a compact and unmodifiable list out of them.
Syntax:
List<Integer> unmodifiableList = List.of(1, 2, 3);
Example: Creating a list using the method List>of() .
import java.util.List;
public class Geeks
{
public static void main(String args[])
{
// Creating a list using List.of()
List<Integer> unmodifiableList = List.of(1, 2, 3);
// Printing the List
System.out.println("List : " + unmodifiableList.toString());
}
}
Output
List : [1, 2, 3]