Home » Java Collections emptyList() Method with Examples

Java Collections emptyList() Method with Examples

by Online Tutorials Library

Java Collections emptyList() Method

The emptyList() method of Java Collections class is used to get a List that has no elements. These empty list are immutable in nature.

Syntax

Following is the declaration of emptyList() method:

Parameter

This method does not accept any parameter.

Returns

The emptyList() method returns an empty immutable list.

Exceptions

NA

Compatibility Version

Java 1.5 and above

Example 1

Test it Now

Output:

Empty list: []  

Example 2

Test it Now

Output:

Created empty immutable list: []  Exception in thread "main" java.lang.UnsupportedOperationException  at java.base/java.util.AbstractList.add(AbstractList.java:153)  at java.base/java.util.AbstractList.add(AbstractList.java:111)  at myPackage.CollectionsEmptyListExample1.main(CollectionsEmptyListExample1.java:9)  

Example 3

Test it Now

Output:

Exception in thread "main" java.lang.UnsupportedOperationException  at java.base/java.util.AbstractList.add(AbstractList.java:153)  at java.base/java.util.AbstractList.add(AbstractList.java:111)  at myPackage.CollectionsEmptyListExample3.main(CollectionsEmptyListExample3.java:8)  

You may also like