Reputation: 8665
Is there a difference between Map.of()
and Collections.emptyMap()
, between List.of()
and Collections.emptyList()
and between Set.of()
and Collections.emptySet()
?
Upvotes: 47
Views: 7105
Reputation: 34313
Yes, there are even behavioral and not just technical differences between the collections returned by the emptyXyz
factory methods in the Collections
class and the new of
factory methods introduced in the interfaces (Map
, List
, Set
) with JDK 9, if these are invoked with no arguments.
The relevant difference is that the collections returned by the new of
factory methods disallow null
elements, keys and values (as pointed out in the API documentation in the List, Set and Map interfaces). This might sound irrelvant for empty collections, but even if it is not quite clearly documented, even the accessor methods in the new collection implementations check for null
values.
Some examples of the differences:
Collections.emptyList().contains(null)
will return false, while List.of().contains(null)
will throw a NullPointerException
.
Collections.emptyMap().getOrDefault(null, V)
will return V
, while Map.of().getOrDefault(null, V)
will throw a NullPointerException
.
As currently implemented in Oracle's JDK 9, at least the following methods on the collections returned by the new factory methods will throw NullPointerException
s, but behave 'sanely' (as in how the collection classes were originally designed and specified to support null keys and values) using the old factory methods in the Collections
class:
List.of().contains(null);
Set.of().contains(null);
Map.of().containsKey(null);
Map.of().containsValue(null);
Map.of().getOrDefault(null, <any>);
Upvotes: 46