mkodekar / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

ImmutableMap should optionally allow null values. #1782

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It would be nice to have an option to allow null values in ImmutableMaps. There 
are perfectly legitimate use cases for maps with null values, e.g. representing 
the properties and values of a bean, some of which may be null-valued. 

Most implementations of Map allow null values, and this restriction 
unnecessarily limits the places where ImmutableMap can be used to replace 
existing small mutable maps, and for no good reason that I can see. Ambiguity 
is not an issue, since there containsKey can resolve the question of whether a 
null value was inserted or not. In cases where map entries are iterated there 
is no ambiguity.

Original issue reported on code.google.com by rattigan@gmail.com on 14 Jun 2014 at 9:26

GoogleCodeExporter commented 9 years ago
What's wrong with the workaround of using Collections.unmodifiableMap on a 
mutable map?

Large amounts of code already assume that anything Immutable* can't have null 
values.  (Additionally, you'd have to extend this to every immutable collection 
type, not just Map.)

Original comment by wasserman.louis on 14 Jun 2014 at 9:30

GoogleCodeExporter commented 9 years ago
https://code.google.com/p/guava-libraries/wiki/UsingAndAvoidingNullExplained

You could also store an Optional<V> as your value.

Original comment by kak@google.com on 15 Jun 2014 at 10:46

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:08

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:07