What is the use of Collections.EMPTY_MAP, EMPTY_LIST, EMPTY_SET

Since they are all the same, why do they need it?

+5
source share
4 answers

This is sometimes the best alternative to returning null.

public List<?> getList(){
    if(list == null){
        return Collections.emptyList();
    }

    return list;
}

Link

+8
source

These are convenient functions: for functions that must return a nonzero value, you can use them, and you do not need to create a new object, and you can perform equality checks on them.

+1
source

:

** // , EMPTY_something List/Map. Collections.java, EmptyList, EmptyMap , ArrayList. HashMap.

** Save JVM heaps in space since there is no initial capacity for these empty collections.

The above may not seem very important, however, an important consideration and best practices.

0
source

Return an immutable list in order; also its even the preferred way, unless the documentation indicates otherwise.

0
source

All Articles