• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashmap with duplicate key.

 
jamil lusa
Ranch Hand
Posts: 59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jamil did you try it out? What was the output when you tried it?
 
jamil lusa
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Jamil.
 
Alexandr Khomichenko
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There will be duplication of keys. See the sample:

output: y y
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic