How to Initialize a List in Java with Best Examples

In this tutorial, we will learn in Java how to initialize a list with the help of an example. A list is an organized set that allows us to save and access elements successively. It consists of index-based methods for inserting, updating, searching, and deleting the elements. It also contains duplicate elements.

The list interface is executed by Array List, Linked List, Vector and Stack classes.

Moreover, the list is indeed an interface and the particular list will be created in the following process:

List x = new ArrayList();
List y = new LinkedList();
List z = new Vector();
List w = new Stack();

Here are the following methods to initialize a list:

List add() Method in Java

Therefore, we already know that the list is an interface, it cannot directly represent it. On the other hand, we can make objects of those classes which instantiate them after implementing this interface.

There are minor classes which have to represent the List interface which are the Stack, Array List, Linked List, Vector and etc.

This is the syntax used:

List<Integer> name=new ArrayList<Integer>();
List<Integer> name1=new LinkedList<Integer>();
List<Integer> stack=new Stack<Integer>();

For Example:

import java.util.*;
import java.util.function.Supplier;
  
public class InitializeMainJava {
    public static void main(String args[])
    {
  
        // For ArrayList
        List<Integer> number = new ArrayList<Integer>();
        number.add(1);
        number.add(13);
        System.out.println("ArrayList : " + number.toString());
  
        // For LinkedList
        List<Integer> numberList = new LinkedList<Integer>();
        numberList.add(2);
        numberList.add(41);
        System.out.println("LinkedList : " + numberList.toString());
  
        // For Stack
        List<Integer> stack = new Stack<Integer>();
        stack.add(3);
        stack.add(11);
        System.out.println("Stack : " + stack.toString());
    }
}

Output:

List add() Method in Java

We can also use the Double Brace Initialization in the example above.

The syntax we will use is given below:

List<Integer> number=new ArrayList<Integer>(){{
add(1);
add(2);
add(3);
}};

Let’s look at the other example below with the use of syntax above:

import java.util.*;
 
public class InitializeMainJava {
    public static void main(String args[])
    {
  
        // For ArrayList
        List<Integer> number = new ArrayList<Integer>();
        number.add(1);
        number.add(13);
        System.out.println("ArrayList : " + number.toString());
  
        // For LinkedList
        List<Integer> numberList = new LinkedList<Integer>();
        numberList.add(2);
        numberList.add(41);
        System.out.println("LinkedList : " + numberList.toString());
  
        // For Stack
        List<Integer> stack = new Stack<Integer>();
        stack.add(3);
        stack.add(11);
        System.out.println("Stack : " + stack.toString());
    }
}

Output:

Arrays.asList() Java

Here are the methods of arrays.asList Java: (1) Immutable List and (2) Mutable List

Immutable List

Arrays.asList() it can make an immutable list in an array. Besides, using this array, it is easy to create a list.

Syntax:

List<Integer> name=Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

For example:

import java.util.Arrays;
import java.util.List;
  
public class ListArray {
    public static void main(String args[])
    {
  
        // Instantiating List using Arrays.asList()
        List<Integer> number = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  
        // Print the list
        System.out.println("List : " + number.toString());
    }
}

Output:

Mutable List Java

This is the syntax we will use:

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

Example in Mutable List in Java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MutableListJava {
public static void main(String args[])
{

    List<Integer> number = new ArrayList<>(
        Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

    System.out.println("List : " + number.toString());

    number.add(5);

    System.out.println("Modified number : " + number.toString());
}

}

Output:

Mutable List Java

Collections Class Methods in Java

There are different methods in Collections class which should be used to instantiate a list. Such as Collections.addAll(), Collections.unmodifiableList(), Using Collections.singletonList(),

Collections.addAll()

A collection class consists of a static method which is called addAll() that may be used to create a list from the scratch. Besides, Collections.addAll() when it is defined with the Collection in which the elements are to be inserted, and it will accept any number of elements.

The syntax used:

List<Integer> number = Collections.EMPTY_LIST;
Collections.addAll(list = new ArrayList<Integer>(), 1, 2, 3, 4);

For example:

import java.util.*;
  
public class CollectionsAddAll {
    public static void main(String args[])
    {
  
        List<Integer> numberlist = new ArrayList<Integer>();
  
        Collections.addAll(numberlist, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  
        System.out.println("List : " + numberlist.toString());
    }
}

Output:

Collections addAll() in Java

Collections.unmodifiableList()

In Java program, A collections.unmodifiableList()  should return a list which cannot be change. It is either adding or deleting an element. In any try to customize the list will result in an Unsupported Operation.

The syntax used:

List<Integer> list = Collections
.unmodifiableList(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

Let’s look at the example below;

import java.util.*;
  
public class unmodifiableList{
    public static void main(String args[])
    {
  
        // Creating the numberlist
        List<Integer> numberlist = Collections.unmodifiableList(
            Arrays.asList(1, 2, 3, 4, 5));
  
        // Print the numberlist
        System.out.println("List : " + numberlist.toString());
    }
}

Output:

Another example:

import java.util.*;
  
public class unmodifiableList {
    public static void main(String args[])
    {
  
        try {
           
            List<Integer> numberlist = Collections.unmodifiableList(
                Arrays.asList(1, 2, 3, 4, 5));
  
            
            System.out.println("List : " + numberlist.toString());
  
            
            System.out.println("Trying to Change the number list");
            numberlist.set(0, numberlist.get(0));
        }
  
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}

Output:

Collections.singletonList() in Java

In Java language, a collections.singletonList() will return an immutable list which is containing one element only.

Syntax Used:

List<Integer> numberlist = Collections.singletonList(2);

For example:

import java.util.*;
  
public class CollectionsSingletonList{
    public static void main(String args[])
    {
        List<Integer> numberlist = Collections.singletonList(2);
        System.out.println("List : " + numberlist.toString());
    }
}

Output:

Stream in Java 8

Since Java 8 has Stream and functional programming, it is now possible to create any stream of objects and then gather them into a list.

Syntax Used:

List<Integer> list 
            = Stream.of(1, 2, 3)
                .collect(Collectors.toList());
List<Integer> list 
            = Stream.of(1, 2, 3)
                .collect(Collectors.toCollection(ArrayList::new));
List<Integer> list 
            = Stream.of(1, 2, 3, 4)
                .collect(Collectors.collectingAndThen(Collectors.toList(), 
                            Collections::unmodifiableList))

For example:

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
  
public class STREAM {
    public static void main(String args[])
    {
        List<Integer> numberSequence1 = Stream.of(1, 2, 3, 4, 5)
                                  .collect(Collectors.toList());
  
        System.out.println("List using Syntax 1: "
                           + numberSequence1.toString());
  
        List<Integer> numberSequence2 = Stream
                                  .of(6, 7, 8, 9, 10)
                                  .collect(
                                      Collectors
                                          .toCollection(ArrayList::new));
  
        System.out.println("List using Syntax 2: "
                           + numberSequence2.toString());
  
        List<Integer> numberSequence3 = Stream
                                  .of(11, 12, 13, 14, 15)
                                  .collect(
                                      Collectors
                                          .collectingAndThen(
                                              Collectors.toList(),
                                              Collections::unmodifiableList));
  
        System.out.println("List using Syntax 3: "
                           + numberSequence3.toString());
    }
}

Output:

Conclusion:

To conclude, we already learn How to Initialize a List in Java and also learn the initialize method such as List add() Method in Java, Arrays.asList() Java, Collections Class Methods in Java and Stream in Java 8.

Leave a Comment