Hashmap get failing

Discussion in 'Java' started by Edward A Thompson, Jun 7, 2004.

  1. I have a hashmap of 13062 items.

    I am using a String object as the key.
    map.get(key) retunrs a null when I search for a string with the value
    "V1272".

    I traced the population of the HashMap, and see "V1272" going in.

    So how/why would the get return null?

    Can anyone give me some debugging hints on this one?

    Ed

    (PS - the key and the value are the same object - is there a smarter
    way to do this?)
    Edward A Thompson, Jun 7, 2004
    #1
    1. Advertising

  2. Edward A Thompson

    Carl Howells Guest

    Edward A Thompson wrote:
    > I have a hashmap of 13062 items.
    >
    > I am using a String object as the key.
    > map.get(key) retunrs a null when I search for a string with the value
    > "V1272".
    >
    > I traced the population of the HashMap, and see "V1272" going in.
    >
    > So how/why would the get return null?


    If the value put in was null, the return value from a succesfull get can
    be null.

    >
    > Can anyone give me some debugging hints on this one?
    >
    > Ed
    >
    > (PS - the key and the value are the same object - is there a smarter
    > way to do this?)


    It sounds like you're not putting the value in that you think you are.
    But try using a HashSet instead. Then you don't need to worry about
    values at all.
    Carl Howells, Jun 7, 2004
    #2
    1. Advertising

  3. On 7 Jun 2004 13:43:32 -0700, Edward A Thompson wrote:

    > I have a hashmap of 13062 items.
    >
    > I am using a String object as the key.
    > map.get(key) retunrs a null when I search for a string with the value
    > "V1272".
    >
    > I traced the population of the HashMap, and see "V1272" going in.


    Are you sure it is "V1272" rather than
    "V1272 " or " V1272"..

    > So how/why would the get return null?
    >
    > Can anyone give me some debugging hints on this one?


    Reduce the map from 13062 items to 2 items
    Do a String.equals(myKey) test on key entry..
    Prepare an SSCCE*, if that in itself does not
    reveal the problem, post it to the group..
    * <http://www.physci.org/codes/sscce.jsp>

    HTH

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, Jun 7, 2004
    #3
  4. Edward A Thompson

    Chris Smith Guest

    Edward A Thompson wrote:
    > I am using a String object as the key.
    > map.get(key) retunrs a null when I search for a string with the value
    > "V1272".
    >
    > I traced the population of the HashMap, and see "V1272" going in.
    >
    > So how/why would the get return null?
    >
    > Can anyone give me some debugging hints on this one?


    Sure, here are a few guesses:

    1. Are you sure it's the same HashMap, or is it possible that you've
    confused something with variable scopes or reassigned a reference since
    the addition?

    2. Do you ever remove anything from the HashMap? If so, is there a
    chance that you're removing this by mistake?

    3. Is the key in the HashMap, but with an explicit null value? What
    does Map.containsKey tell you?

    4. Are you sure the String is really "V1272"? Maybe it's "v1272" or
    "V1272 " instead.

    > (PS - the key and the value are the same object - is there a smarter
    > way to do this?)


    If you don't need the Map functionality, then it's easier to use a
    HashSet instead.

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

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Jun 7, 2004
    #4
  5. Edward A Thompson

    Ed Guest

    OK, I've done a variation on the above suggestions:

    1) changed all my impelmantions to interfaces (should have done that
    before! - replaced HashSet x = new HashSet with Set x = new HashSet() )
    2) Changed all my Maps to Sets.
    3) added debug code to display the hash code going in the HashSet and
    the hash code that fails the contains (variation on the String.equals
    suggestion). We'll see what this shows me.

    I can't run it until tomorrow, but I'll let you know what I was doing wrong.

    thanx for the help!
    Ed, Jun 8, 2004
    #5
  6. Ed <> scribbled the following:
    > OK, I've done a variation on the above suggestions:


    > 1) changed all my impelmantions to interfaces (should have done that
    > before! - replaced HashSet x = new HashSet with Set x = new HashSet()

    )

    Definitely good practice. The Java interface construct is the best
    thing since sliced bread in my opinion. Have a look at the Collections
    and JAXP frameworks for an example of how well it can be used. IMO all
    variables should always be defined as the *least* specific type that is
    needed, to avoid tying yourself to a specific implementation with extra
    functionality you do not really need.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "That's no raisin - it's an ALIEN!"
    - Tourist in MTV's Oddities
    Joona I Palaste, Jun 8, 2004
    #6
  7. Edward A Thompson

    Ed Thompson Guest

    OK, it turns out it was an extraneous space, despite my claims to the
    contrary.

    Thanx for all the suggestions.
    Ed Thompson, Jun 9, 2004
    #7
  8. Edward A Thompson

    Ed Thompson Guest

    I swept the code last night, and loved the results from a design
    perspective.

    The only thing I might have done is define a Set where a Collection
    would have done.
    Ed Thompson, Jun 9, 2004
    #8
  9. Edward A Thompson

    Tony Morris Guest

    "Edward A Thompson" <> wrote in message
    news:...
    > I have a hashmap of 13062 items.
    >
    > I am using a String object as the key.
    > map.get(key) retunrs a null when I search for a string with the value
    > "V1272".
    >
    > I traced the population of the HashMap, and see "V1272" going in.
    >
    > So how/why would the get return null?
    >
    > Can anyone give me some debugging hints on this one?
    >
    > Ed
    >
    > (PS - the key and the value are the same object - is there a smarter
    > way to do this?)


    First and most obvious speculation: Failure to implements equals/hashCode
    correctly

    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform
    Tony Morris, Jun 9, 2004
    #9
  10. Edward A Thompson

    Dale King Guest

    Hello, Edward A Thompson!
    You wrote:

    > I have a hashmap of 13062 items.
    >
    > I am using a String object as the key.
    > map.get(key) retunrs a null when I search for a string with

    the value
    > "V1272".
    >
    > I traced the population of the HashMap, and see "V1272" going

    in.
    >
    > So how/why would the get return null?
    >
    > Can anyone give me some debugging hints on this one?
    >
    > Ed
    >
    > (PS - the key and the value are the same object - is there a

    smarter
    > way to do this?)


    If the key and the value are the same object, wouldn't it make
    more sense to use a HashSet? Implementation-wise it makes little
    difference because a HashSet actually implemented using a
    HashMap.
    --
    Dale King
    My Blog: http://daleking.homedns.org/Blog
    Dale King, Apr 15, 2006
    #10
    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. Vince Darley
    Replies:
    4
    Views:
    4,383
    emilchacko
    Mar 2, 2010
  2. Replies:
    14
    Views:
    13,382
    Alexander
    Jul 20, 2006
  3. Rakesh
    Replies:
    10
    Views:
    12,144
    Mike Schilling
    Apr 8, 2008
  4. ordneal
    Replies:
    0
    Views:
    749
    ordneal
    Jun 10, 2009
  5. Wojtek

    HashMap get/put

    Wojtek, Oct 28, 2009, in forum: Java
    Replies:
    49
    Views:
    2,944
    Jim Janney
    Nov 3, 2009
Loading...

Share This Page