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
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:
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.
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.
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.