• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

HashMap key collision they appear to be very different ?

 
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[]{"",""});
 
Master Rancher
Posts: 3696
44
  • 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?
 
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
Master Rancher
Posts: 3696
44
  • 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:
 
Is that a spider in your hair? Here, threaten it with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic