collection API and Null elements

Discussion in 'Java' started by Murat Tasan, May 6, 2005.

  1. Murat Tasan

    Murat Tasan Guest

    in a Set (and other Collection sub-interfaces), there are some rather
    silly declarations for the jdk API.

    for example, the Set.contains(Object) method throws NullPointerException
    if, "the specified element is null and this set does not support null
    elements (optional)."

    while i understand why this is optional, it makes headaches for a
    definition like this for a method like contains(). if anyone is accepting
    a general Set into a custom method then, and i try to search for a null
    element, i now have to worry about catching an exception.

    such a declaration certainly makes sense for modification methods (like
    add()), but does anyone know what the rationale is for this design in
    non-modification methods (like contains())?

    thanks for any insight,

    murat
     
    Murat Tasan, May 6, 2005
    #1
    1. Advertising

  2. Murat Tasan wrote:

    > for example, the Set.contains(Object) method throws NullPointerException
    > if, "the specified element is null and this set does not support null
    > elements (optional)."
    >
    > while i understand why this is optional, it makes headaches for a
    > definition like this for a method like contains(). if anyone is accepting
    > a general Set into a custom method then, and i try to search for a null
    > element, i now have to worry about catching an exception.


    Possibly. You can also specify in your method's docs that the Set
    argument is required to support null elements, and that the method may
    throw NPE if it does not.

    > such a declaration certainly makes sense for modification methods (like
    > add()), but does anyone know what the rationale is for this design in
    > non-modification methods (like contains())?


    A Collection implementation that cannot contain null elements for some
    reason may not be able to test for them for the same reason. For
    instance, consider a simplified HashSet without the special-case support
    for null that java.util.HashSet has. To determine whether an instance
    contains some object, that object's hash code must be computed, but
    you'll get an NPE if you try to invoke hashCode() on a null reference.

    --
    John Bollinger
     
    John C. Bollinger, May 6, 2005
    #2
    1. Advertising

  3. Murat Tasan

    Chris Smith Guest

    John C. Bollinger <> wrote:
    > A Collection implementation that cannot contain null elements for some
    > reason may not be able to test for them for the same reason. For
    > instance, consider a simplified HashSet without the special-case support
    > for null that java.util.HashSet has. To determine whether an instance
    > contains some object, that object's hash code must be computed, but
    > you'll get an NPE if you try to invoke hashCode() on a null reference.


    That strikes me as a weak argument. If the implementation could not
    contain null references, then the code would just look like this:

    public boolean contains(Object o)
    {
    if (o == null) return false;

    ...
    }

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, May 6, 2005
    #3
  4. Murat Tasan

    Murat Tasan Guest

    On Fri, 06 May 2005 16:56:15 -0600, Chris Smith wrote:

    > John C. Bollinger <> wrote:
    >> A Collection implementation that cannot contain null elements for some
    >> reason may not be able to test for them for the same reason. For
    >> instance, consider a simplified HashSet without the special-case support
    >> for null that java.util.HashSet has. To determine whether an instance
    >> contains some object, that object's hash code must be computed, but
    >> you'll get an NPE if you try to invoke hashCode() on a null reference.

    >
    > That strikes me as a weak argument. If the implementation could not
    > contain null references, then the code would just look like this:
    >
    > public boolean contains(Object o)
    > {
    > if (o == null) return false;
    >
    > ...
    > }


    exactly, this is basically what i would expect any such class to do. this
    simple additions removes the need to constantly check for NPEs when using
    the sub-interfaces of Collection.

    i guess i should really try directing this question to the API
    designers/implementors at Sun. i don't feel confident i'll get an answer,
    though. has anyone else ever sent a similar high-level design question to
    a big company and gotten a response?
     
    Murat Tasan, May 7, 2005
    #4
  5. Murat Tasan

    Chris Smith Guest

    Murat Tasan <> wrote:
    > i guess i should really try directing this question to the API
    > designers/implementors at Sun. i don't feel confident i'll get an answer,
    > though. has anyone else ever sent a similar high-level design question to
    > a big company and gotten a response?


    I doubt they'll write you an essay on design considerations. They'll
    probably reject the suggested change as well, since it could break old
    code.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, May 7, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    26,949
    Mike Schilling
    Mar 29, 2006
  2. Dylan
    Replies:
    5
    Views:
    432
    Daniel T.
    Mar 22, 2005
  3. Pradeep
    Replies:
    2
    Views:
    687
    Patricia Shanahan
    Jan 24, 2007
  4. Øyvind Isaksen
    Replies:
    1
    Views:
    1,001
    Øyvind Isaksen
    May 18, 2007
  5. Hemant

    create collection of collection

    Hemant, Oct 22, 2009, in forum: ASP .Net
    Replies:
    1
    Views:
    428
    Gregory A. Beamer
    Oct 22, 2009
Loading...

Share This Page