Friday, March 13, 2026
HomeLanguagesJavaJava Collections emptyList() Method with Examples

Java Collections emptyList() Method with Examples

The emptyList() method of Java Collections returns the list with no elements. This method is immutable. That is, we can not do any modifications after creating this method.

Syntax:

public static final <T> List<T> emptyList()  

Parameters: It will not accept any parameters

Return: This method will return an empty list

Example 1: 

Java




// Java program to create an empty list
import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
        // create a list that is empty
        List<String> mylist
            = Collections.<String>emptyList();
             
        // display the list
        System.out.println(mylist);
    }
}


Output

[]

Example 2:

Java




// Java program to show an exception
// when adding the elements to the list
import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
        // create a list that is empty
        List<Integer> mylist
            = Collections.<Integer>emptyList();
 
        // add 5 elements to the created list
        mylist.add(1);
        mylist.add(2);
        mylist.add(3);
        mylist.add(4);
        mylist.add(5);
 
        // display the list
        System.out.println(mylist);
    }
}


Output:

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(AbstractList.java:148)
    at java.util.AbstractList.add(AbstractList.java:108)
    at GFG.main(GFG.java:11)
RELATED ARTICLES

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS