[concurrency-interest] Handling Null Values in ConcurrentHashMap

Tim Peierls tim at peierls.net
Fri May 12 14:28:16 EDT 2006


So how about something like this?

public enum Null {
    VALUE;
    public static T null() { return (T) VALUE; }
    public static boolean isNull(T x) { return x == VALUE; }
}

--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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20060512/6bfdb8dc/attachment.html


More information about the Concurrency-interest mailing list