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.

The Collections.emptyList() method in Java, part of the java.util.Collections class, returns an immutable empty List. This means that the list cannot be modified after it is created, ensuring that it remains empty throughout its lifecycle.

The primary use case for emptyList() is when you need to return a list from a method or assign an empty list to a variable, but you want to ensure that the list cannot be modified accidentally. By using an immutable empty list, you can guarantee that the list will remain empty and unchanged.

Syntax

Following is the declaration of emptyList() method:

Parameter

The 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

CollectionsEmptyListExample1.java

Test it Now

Output:

Empty list: []

One important feature of emptyList() is that it returns the same instance of an empty list every time it is called. This means that if you call emptyList() multiple times, you will always get the same list object. The behavior can be useful for saving memory, as you only need one instance of an empty list in your program.

Since the returned list is immutable, attempting to modify it using methods like add() or remove() will result in an UnsupportedOperationException. This ensures that the empty list remains empty and cannot be changed, further guaranteeing its immutability.

Example 2

CollectionsEmptyListExample2.java

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

CollectionsEmptyListExample3.java

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)

Let's understand about Java Collections emptyList() Method in detail with the help of few Java example programs.

Example 1:

Filename: EmptyListExample.java

Output:

An immutable empty list has been created: []
Failed to modify the empty list: null
Is the empty list still empty? true
Are both empty lists the same instance? true
Failed to modify the second empty list: null
Is the second empty list still empty? true
Second empty list after attempted modifications: []

In summary, the Collections.emptyList() method is a convenient way to obtain an immutable empty list in Java, useful for scenarios where you need an empty list that cannot be modified.

In summary, the Collections.emptyList() method is a convenient way to obtain an immutable empty list in Java, useful for scenarios where you need an empty list that cannot be modified.






Latest Courses