[concurrency-interest] Handling Null Values in ConcurrentHashMap

Tim Peierls tim at peierls.net
Sat May 13 08:22:23 EDT 2006


David Holmes says that a retraction is in order. He points out that the code
I posted not only doesn't compile, it doesn't even work if you fix the code!
You may be able to insert Null-ish values into collections of other types,
but when you iterate over the collection, you'll get a ClassCastException.

So I take back the suggestion. Generics are tricky. Stick to non-null values
(and keys!) in collections and maps. Try to rewrite code that depends on
sticking nulls in collections to use the Null *pattern* instead -- that's
the one where you provide a special implementation of your interface that
can be tested for nullity. It preserves type-safety and is strongly
recommended over using the primitive null value.

Next time I'll write a test program before mouthing off. :-(

--tim

On 5/12/06, Tim Peierls <tim at peierls.net> wrote:
>
> And I would certainly recommend avoiding null in the first place over any
> of this. This was all in the context of Doug's reaction to an expressed need
> to deal with null values in collections.
>
>
> --tim
>
> On 5/12/06, Bob Lee <crazybob at crazybob.org> wrote:
> >
> > Hey! I didn't recommend that. ;)
> >
> > In the blog I linked to, I said:
> >
> > "Things can still get a little hairy when you mix null placeholders
> > with generic types. Your placeholder can implement the same type as
> > the value, or you can resort to casting hacks."
> >
> > OK, I said, "you can," but I wouldn't do it myself.
> >
> > The nice thing about the enum null placeholder is that it's so short
> > and sweet you don't have to refactor it out into a common place and
> > risk infecting others with bad ideas. ;)
> >
> > Bob
> >
> > On 5/12/06, Joshua Bloch <josh at bloch.us> wrote:
> > > Tim and Bob,
> > >
> > > These are interesting/cool patterns, but they're a bit scary, as they
> > > cast something that isn't a T to (T).  You have to be very careful
> > > what you do with the the pseudo-null.  I can see using this inside a
> > > library, but I would think more than twice before putting it in the
> > > JDK.
> > >
> > >        Josh
> > >
> > > On 5/12/06, Tim Peierls <tim at peierls.net> wrote:
> > > > So how about something like this?
> > > >
> > > >
> > > > public enum Null {
> > > >     VALUE;
> > > >     public static T value() { return (T) VALUE; }
> > > >     public static boolean isNull(T value) { return value == VALUE; }
> > > > }
> > > >
> > > > Use Null.<Foo>value() in Set<Foo>, but don't forget to check
> > > > Null.isNull(foo).
> > > >
> > > > --tim
> > > >
> > > >
> > > > --tim
> > > >
> > > >
> > > > On 5/12/06, Bob Lee <crazybob at crazybob.org> wrote:
> > > > > The nice part about using an enum is that everything works even
> > after
> > > > > you serialize and deserialize your object.
> > > > >
> > > > > Bob
> > > > >
> > > > > On 5/12/06, Tim Peierls <tim at peierls.net> wrote:
> > > > > > On 5/12/06, Bob Lee < crazybob at crazybob.org> wrote:
> > > > > >
> > > > > > > On 5/12/06, Doug Lea <dl at cs.oswego.edu> wrote:
> > > > > > > > Would it be easier to declare somewhere
> > > > > > > >    static final Object NULL = new Object();
> > > > > > > > and replace all use of nulls in uses of maps with NULL?
> > > > > > >
> > > > > > > Enums also work great here:
> > > > > > >
> > > > > >
> > > > http://crazybob.org/2005/12/null-placeholders-in-jdk-15.html
> > > > > >
> > > > > >
> > > > > > And for getting around in a generics-enabled world:
> > > > > >
> > > > > > public class Null<T> {
> > > > > >     private static final Object NULL = new Object();
> > > > > >     public static T null() { return (T) NULL; }
> > > > > >     public static boolean isNull(T x) { return x == NULL; }
> > > > > >     private Null() {}
> > > > > > }
> > > > > >
> > > > > > Get a null value for type Foo with Null.<Foo>null(). Test it
> > with
> > > > > > Null.isNull(foo) or get CCEs in the same way that you would get
> > NPE if
> > > > you
> > > > > > were using primitive null.
> > > > > >
> > > > > > --tim
> > > > >
> > > >
> > > >
> > > > _______________________________________________
> > > > Concurrency-interest mailing list
> > > > Concurrency-interest at altair.cs.oswego.edu
> > > > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
> > > >
> > > >
> > > >
> > >
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20060513/ca09289f/attachment-0001.html


More information about the Concurrency-interest mailing list