Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any practical application/use case when we create empty immutable list / set / map

Java 9 provides as a way to create Empty immutable List, set and Map.

List list = List.of(); 
Set set = Set.of(); 
Map map = Map.of();

But I am not able understand what is the actual use case of creating an empty immutable list / set / map.

Please help me to understand the actual use case of an empty immutable list / set / map.

like image 698
Amit Garg Avatar asked Mar 03 '18 15:03

Amit Garg


People also ask

What is the use of empty map in Java?

The emptyMap() method is used to get the empty map (immutable). This map is serializable.


2 Answers

Just imagine a regular mathematical operation that is supposed to operate on those collections. Like computing the intersection of lists. The result can be empty, in this case this method would be of good use if the result should be immutable.

public List<E> intersectLists(List<E> first, List<E> second) {
    // If one is empty, return empty list
    if (first.isEmpty() || second.isEmpty()) {
        // Before Java 9: return Collections.emptyList();
        return List.of();
    }

    // Compute intersection
    ...
}

Immutable collections are often used when you expose internal data structures through getters but don't want the caller to be able to manipulate the collection.

A similar variant are unmodifiable collections. Those can be manipulated, if you have a direct reference to the mutable collection lying underneath the wrapper. By that you can force a user to use your designated methods for manipulation.

public Graph {
    private List<Node> nodes;

    // Other stuff
    ...

    public List<Node> getNodes() {
        // Wrap container to make list unmodifiable
        return Collections.unmodifiableList(nodes);
    }

    // User should use designated methods to manipulate instead
    public void addNode(Node node) { ... }
    public void removeNode(Node node) { ... }
}
like image 165
Zabuzard Avatar answered Sep 30 '22 10:09

Zabuzard


Well before Java 9, there were Collections.emptyList(), Collections.emptySet(), and Collections.emptyMap(), which are explicitly and solely dedicated to producing unmodifiable empty collections. That List.of() etc. can also do this is a matter of internal consistency of their API, not a revolutionary new feature.

But your main question seems to be

Please help me to understand the actual use case of an empty immutable list / set / map.

This is really three questions:

  1. What are the use cases of empty collections?

    Empty collections are useful because they are natural representations of situations that occur in real-world data and algorithms. For example, "Which passengers have checked in so far?" when none have. Which tasks are waiting to be dispatched? I could continue endlessly.

  2. What are the use cases of unmodifiable collections?

    A reference to an unmodifiable collection can be passed around without fear that the collection will be modified (via that reference, anyway). Note well that supposing the unmodifiability is appropriately documented, all participants in sharing that object can rely on it not to change. Among other things, this is important for safely sharing collections (and other unmodifiable objects) without making copies of them.

  3. What use cases are in the intersection of those?

    Empty collections occur naturally in real-world data and algorithms, so most use cases for unmodifiable collections are inclusive of empty collections.


Additionally, you did not ask but are perhaps overlooking that although you cannot add objects to an unmodifiable empty collection, you can replace it altogether with a non-empty collection, or serve up a non-empty collection the next time, or similar.

like image 38
John Bollinger Avatar answered Sep 30 '22 11:09

John Bollinger