This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashmap with duplicate key. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashmap with duplicate key." Watch "hashmap with duplicate key." New topic

hashmap with duplicate key.

jamil lusa
Ranch Hand

Joined: Aug 18, 2011
Posts: 59
Hi, good day everyone,

i am reading the K&b scjp 6 guide book, in this example:

after that i have read this section:

so obviously changing the hashcode-factor will affect the hashmap collection object (from the example above), now my question is
if the hashcode is changed, it is possible to get duplicated key, and what will happen since hashmap does not allow dulplicated key?

I have checked the hashmap API , the function 'put' will override/replace the duplicated value with the previous value. but how about the question taht i am asking? the collection has distinct key, but suddenly its hashcode has been changed. what happen to the collection?

Thanks , appreciate your reply!
Ankit Garg

Joined: Aug 03, 2008
Posts: 9452

Jamil did you try it out? What was the output when you tried it?

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
jamil lusa
Ranch Hand

Joined: Aug 18, 2011
Posts: 59
Hi, sorry i do have compiler in my office pc right now. if got, i have tried to compile it already.

i am using my spare time in office to learn scjp. hoho , hope not be caught by my boss.

this question is too tricky, i really cannot figure out.

hope this question is interesting enough, so there is someone may test it.

Good day,
Alexandr Khomichenko

Joined: Oct 13, 2011
Posts: 1
There will be duplication of keys. See the sample:

output: y y
It is sorta covered in the JavaRanch Style Guide.
subject: hashmap with duplicate key.
It's not a secret anymore!