emptyList() is a built-in static method of the Java Collections class. This method doesn’t accept any parameter. It creates an empty and immutable List. It is a type-safe way of creating a list without explicitly creating a new instance of an empty List. Additionally, the Collections class offers an EMPTY_LIST constant that serves the same purpose as the emptyList() method.
This Java guide presents a comprehensive overview of the emptyList() method using suitable examples.
Quick Outlines
- How to Create an Empty List via the emptyList() Method in Java?
- How to Create an Empty List Using EMPTY_LIST Constant in Java?
- How to Create an Empty List Using a new List Instance in Java?
- emptyList() Vs. EMPTY_LIST Vs. new List – What’s the Difference?
- Final Thoughts
How to Create an Empty List via the emptyList() Method in Java?
Java users use the Collections emptyList() method when they want to create an empty list that remains the same/empty throughout the code. To create an empty list using the emptyList() method, use the below syntax:
public static final <dataType> List<dataType> emptyList();
Example: Creating Empty List via emptyList()
First, import the required classes, such as Collections, and List. Both these classes belong to the “java.util” package so we can import them using a single import statement as follows:
import java.util.*;
Next, we use the emptyList() method to create a new string-type list:
List<String> newList = Collections.emptyList();
We use the size() method on the created list to verify its size:
System.out.println("Size of newList: " + newList.size());
Output
Example 2: UnsupportedOperationException
Since the emptyList() method is immutable in nature, so trying to modify it will result in an exception. For instance, in the following code, we create an empty list using the emptyList() method and then try to add a couple of values to it:
As a result, we encountered the UnsupportedOperationException:
How to Create an Empty List Using EMPTY_LIST Constant in Java?
The “Collections” class provides a static constant “EMPTY_LIST” that retrieves an empty list. It allows us to create an empty list without specifying the data type of the List:
List newList = Collections.EMPTY_LIST;
Example: Creating Empty List Using EMPTY_LIST
In this example, we create a new list using the EMPTY_LIST property and use the size() method to verify its creation:
List newList = Collections.EMPTY_LIST;
System.out.println("List Size: "+ newList.size());
The output snippet shows the size of the created list:
The EMPTY_LIST property throws the same “UnsupportedOperationException” if we try to modify it:
How to Create an Empty List Using a new List Instance in Java?
If you want to create a mutable empty list that can be modified at any time, use the new list instance instead of the emptyList() method:
List<dataType> listName = new ArrayList<>();
Example 1: Create an Empty List
In this example, first, we create an empty list using new list instance:
List<String> empNames = new ArrayList<>();
Next we ensure the list creation by checking its size:
System.out.println("List Size: "+ empNames.size());
Now add the elements into the newly created empNames list:
System.out.println("Add Elements to the list: ");
empNames.add("Joseph");
empNames.add("David");
empNames.add("Roman");
Finally, use the size() method one more time to check the updated list size:
System.out.println("Modified List Size: "+ empNames.size());
Output
emptyList() Vs. EMPTY_LIST Vs. new List – What’s the Difference?
The emptyList() method and EMPTY_LIST constant are immutable in nature while the new List instance is mutable. This means an empty list created with the emptyList() method or EMPTY_LIST constant can’t be modified at any time. An empty list created using the new List instance can be modified according to the users’ preferences. The key differences among all these approaches are listed in the following table:
Factor | emptyList() | EMPTY_LIST | new List |
---|---|---|---|
Syntax | List<dataType> listName = Collections.emptyList(); | List listName = Collections.EMPTY_LIST; | List<dataType> listName = new ArrayList<>(); |
Class | Collections. | Collections. | Implements List interface. |
Mutability | Immutable (Can’t be updated/read-only) | Immutable (Can’t be updated/read-only) | Mutable (can be updated) |
Type-Safe | Yes | Retrieves a raw List type. | Yes |
Possible Exception | UnsupportedOperationException | UnsupportedOperationException | Doesn’t Throw UnsupportedOperationException |
It is recommended to use the emptyList() method if you want to create an empty list and intend to maintain its immutability throughout the program. However, if you want to create an empty list that can be modified at any instant, use the new List instant.
This is how the emptyList() method of the Collections class works in Java.
Final Thoughts
In Java, the emptyList() is a built-in static method of the Collections class that doesn’t accept any parameter. It creates an empty and immutable List. Alternatively, the EMPTY_LIST constant and new List instance are used in Java. However, there are some differences between the “emptyList()” method, the “EMPTY_LIST” constant, and the “new List” instance with respect to various factors, such as mutability, type-safety, syntax, etc. The main difference is that the emptyList(), and EMPTY_LIST are immutable while the new List instance is mutable. This Java guide has illustrated different examples to explain the working of the emptyList() method.