• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMap key collision they appear to be very different ?  RSS feed

 
steve labar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had the following test collide and can't seem to explain it:

String one = "ctl00$ctl00$contentBody$cphMain$RadDockA$C$tbPassword";
System.out.println(one.hashCode());
String two = "ctl00$ctl00$contentBody$cphMain$RadDockA$C$tbLoginID";
System.out.println(two.hashCode() + "" +one.equals(two));
HashMap<String,String[]> m = new HashMap<String,String[]>();
m.put(one, new String[]{"",""});
m.put(two, new String[]{"",""});

The map overwrites the key? I just don't understand why? When I print out the hashcodes they are diferent and equals on these two should return false. Any ideas what is going on and how i can fix this in the future.
 
steve labar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two more strings overwritting eachother? what in the world is going on?

String three ="WEBRPTID";
String four = "USER";
HashMap<String,String[]> m = new HashMap<String,String[]>();
m.put(three, new String[]{"",""});
m.put(four, new String[]{"",""});
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
steve labar wrote:The map overwrites the key? I just don't understand why?

I don't know what you mean here. The map doesn't seem to do anything that "overwrites" a key. What makes you think that it does?

steve labar wrote:When I print out the hashcodes they are diferent and equals on these two should return false.

And when you print the result of equals(), it is false. So this seems correct.

It's kind of hard to see the output clearly here, since you printed the result of the equals() at the end of another line, with no meaningful label. It might help if you print this on a new line with a label. Try something like this:


steve labar wrote:Any ideas what is going on and how i can fix this in the future.

What makes you think anything is broken?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
steve labar wrote:
The map overwrites the key? I just don't understand why? When I print out the hashcodes they are diferent and equals on these two should return false. Any ideas what is going on and how i can fix this in the future.


I see no evidence for over-writing here.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
steve labar wrote:I have two more strings overwritting eachother?

Not in the code you show here.

steve labar wrote:what in the world is going on?

String three ="WEBRPTID";
String four = "USER";
HashMap<String,String[]> m = new HashMap<String,String[]>();
m.put(three, new String[]{"",""});
m.put(four, new String[]{"",""});

Um, well, you create some strings, and use them as keys in a HashMap. You can see the contents of the HashMap if you simply print out m:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!