Hashmap get failing

  • Thread starter Edward A Thompson
  • Start date
E

Edward A Thompson

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?)
 
C

Carl Howells

Edward said:
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.
 
A

Andrew Thompson

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
 
C

Chris Smith

Edward said:
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
 
E

Ed

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!
 
J

Joona I Palaste

Ed said:
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.
 
E

Ed Thompson

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

Thanx for all the suggestions.
 
E

Ed Thompson

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.
 
T

Tony Morris

Edward A Thompson said:
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
 
D

Dale King

Hello, Edward A Thompson!
You said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top