Java Collections emptyList() Method with Examples
Last Updated :
13 Jan, 2022
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
import java.util.*;
public class GFG {
public static void main(String[] args)
{
List<String> mylist
= Collections.<String>emptyList();
System.out.println(mylist);
}
}
|
Example 2:
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
List<Integer> mylist
= Collections.<Integer>emptyList();
mylist.add( 1 );
mylist.add( 2 );
mylist.add( 3 );
mylist.add( 4 );
mylist.add( 5 );
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)
Share your thoughts in the comments
Please Login to comment...