有没有之间的差异Map.of()
和Collections.emptyMap()
之间List.of()
和Collections.emptyList()
之间Set.of()
和Collections.emptySet()
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
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
.
Collection.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>);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.