Java Programming Tutorials

Java programming tutorials with many code examples!

Java empty List/Collection and null check


In Java empty List/Collection is very often used to implement NullObject Pattern and make code more readable. See how on the following example.


Continuing by example saga, here’s how Collections.emptyList() may be used to implement NullObject pattern and hence help to write maintainable code:


import java.util.Arrays;
import java.util.Collections;
import java.util.List;

class SampleService {

    public List<Integer> retrieveData(String someParam) {
        // do processing only when have valid parameter:
        if (validate(someParam)) {
            // return default value with generic type inferred:
            return Collections.emptyList();
        return Arrays.asList(1, 1, 2, 3, 5, 8);

    private boolean validate(String someParam) {
        return someParam == null;

public class CollectionsEmptyCollection {

    public static void main(String[] args) {
        SampleService service = new SampleService();

        List<Integer> data = service.retrieveData("42");

        data = service.retrieveData(null);

        System.out.println("Adding to unmodifiable:");

    private static void display(List<Integer> data) {
        System.out.println("Loaded data: " + data);
        // Clean code without null checks!
        for (Integer i : data) {

Of course this is a bit contrived example, but shows that empty list can be used to reduce null checks in code. Note that client of the retrieveData method can process data in uniform way, without having to check for nulls.

The last part shown that Collections.emptyList returns unmodifiable list, so if you plan to modify the result better return new List or you will receive UnsupportedOperationException as in the result below.

As always, result of running the Java example:

Loaded data: [1, 1, 2, 3, 5, 8]
Loaded data: []
Adding to unmodifiable:
Exception in thread "main" java.lang.UnsupportedOperationException
      at java.util.AbstractList.add(
      at java.util.AbstractList.add(

java.util.Collections has empty methods for different types of collections (List/Set/Map/Enumeration):

  • <T> Enumeration<T> emptyEnumeration() (since Java 7)
    Returns an enumeration that has no elements.
  • <T> Iterator<T> emptyIterator() (since Java 7)
    Returns an iterator that has no elements.
  • <T> List<T> emptyList() (since Java 5)
    Returns an empty list (immutable).
  • <T> ListIterator<T> emptyListIterator() (since Java 7)
    Returns a list iterator that has no elements.
  • <K,V> Map<K,V> emptyMap() (since Java 5)
    Returns an empty map (immutable).
  • <K,V> NavigableMap<K,V> emptyNavigableMap() (since Java 8)
    Returns an empty navigable map (immutable).
  • <E> NavigableSet<E> emptyNavigableSet() (since Java 8)
    Returns an empty navigable set (immutable).
  • <T> Set<T> emptySet() (since Java 5)
    Returns an empty set (immutable).
  • <K,V> SortedMap<K,V> emptySortedMap() (since Java 8)
    Returns an empty sorted map (immutable).
  • <E> SortedSet<E> emptySortedSet() (since Java 8)
    Returns an empty sorted set (immutable).

It seems a lot, but the concept is the same. ;-)

Was it helpful? Share!