Initializing a List in Java

The Java.util.List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack classes.
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();
Below are the following ways to initialize a list:
-
Using List.add() method
Since list is an interface, one can’t directly instantiate it. However, one can create objects of those classes which have implemented this interface and instantiate them.
Few classes which have implemented the List interface are Stack, ArrayList, LinkedList, Vector etc.
Syntax:
List<Integer> list=new ArrayList<Integer>(); List<Integer> llist=new LinkedList<Integer>(); List<Integer> stack=new Stack<Integer>();
Examples:
importjava.util.*;importjava.util.function.Supplier;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String args[])   Â{       Â// For ArrayList       ÂList<Integer> list =newArrayList<Integer>();       Âlist.add(1);       Âlist.add(3);       ÂSystem.out.println("ArrayList : "+ list.toString());       Â// For LinkedList       ÂList<Integer> llist =newLinkedList<Integer>();       Âllist.add(2);       Âllist.add(4);       ÂSystem.out.println("LinkedList : "+ llist.toString());       Â// For Stack       ÂList<Integer> stack =newStack<Integer>();       Âstack.add(3);       Âstack.add(1);       ÂSystem.out.println("Stack : "+ stack.toString());   Â}}Output:ArrayList : [1, 3] LinkedList : [2, 4] Stack : [3, 1]
Double Brace Initialization can also be used to do the above work.
Syntax:
List<Integer> list=new ArrayList<Integer>(){{ add(1); add(2); add(3); }};Examples:
importjava.util.*;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String args[])   Â{       Â// For ArrayList       ÂList<Integer> list =newArrayList<Integer>() {{           Âadd(1);           Âadd(3);           Â} };       ÂSystem.out.println("ArrayList : "+ list.toString());       Â// For LinkedList       ÂList<Integer> llist =newLinkedList<Integer>() {{           Âadd(2);           Âadd(4);           Â} };       ÂSystem.out.println("LinkedList : "+ llist.toString());       Â// For Stack       ÂList<Integer> stack =newStack<Integer>() {{           Âadd(3);           Âadd(1);           Â} };       ÂSystem.out.println("Stack : "+ stack.toString());   Â}}Output:ArrayList : [1, 3] LinkedList : [2, 4] Stack : [3, 1]
-
Using Arrays.asList()
- Creating Immutable List
Arrays.asList() 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);
Examples:
importjava.util.Arrays;importjava.util.List;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String args[])   Â{       Â// Instantiating List using Arrays.asList()       ÂList<Integer> list = Arrays.asList(1,2,3);       Â// Print the list       ÂSystem.out.println("List : "+ list.toString());   Â}}Output:List : [1, 2, 3]
- Creating Mutable List
Syntax:
List<Integer> list=new ArrayList<>(Arrays.asList(1, 2, 3));
Examples:
importjava.util.ArrayList;importjava.util.Arrays;importjava.util.List;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String args[])   Â{       Â// Creating a mutable list using Arrays.asList()       ÂList<Integer> list =newArrayList<>(           Â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]
- Creating Immutable List
-
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);
Examples:
importjava.util.*;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String args[])   Â{       Â// Create an empty list       ÂList<Integer> list =newArrayList<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:
importjava.util.*;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(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:
importjava.util.*;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(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 1:
importjava.util.*;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String args[])   Â{       Â// Creating the list       ÂList<Integer> list = Collections.singletonList(2);       Â// Print the list       ÂSystem.out.println("List : "+ list.toString());   Â}}Output:List : [2]
-
-
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));Examples:
importjava.util.*;importjava.util.stream.Collectors;importjava.util.stream.Stream;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String args[])   Â{       Â// Creating a List using Syntax 1       ÂList<Integer> list1 = Stream.of(1,2,3)                                 Â.collect(Collectors.toList());       Â// Printing the list       ÂSystem.out.println("List using Syntax 1: "                          Â+ list1.toString());       Â// Creating a List using Syntax 2       ÂList<Integer> list2 = Stream                                 Â.of(3,2,1)                                 Â.collect(                                     ÂCollectors                                         Â.toCollection(ArrayList::new));       Â// Printing the list       ÂSystem.out.println("List using Syntax 2: "                          Â+ list2.toString());       Â// Creating a List using Syntax 3       ÂList<Integer> list3 = Stream                                 Â.of(1,2,3,4)                                 Â.collect(                                     ÂCollectors                                         Â.collectingAndThen(                                             ÂCollectors.toList(),                                             ÂCollections::unmodifiableList));       Â// Printing the list       ÂSystem.out.println("List using Syntax 3: "                          Â+ list3.toString());   Â}}Output:List using Syntax 1: [1, 2, 3] List using Syntax 2: [3, 2, 1] List using Syntax 3: [1, 2, 3, 4]
-
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);
Examples:
importjava.util.List;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(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:
[1, 2, 3]




