• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMaps

 
Komal Arora
Ranch Hand
Posts: 91
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just a simple question: Do hashMaps allow duplicate keys? can there be distinct keys but duplicate values? is Hashmap sorted or ordered?
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, that's three questions! ;)
The java.util.HashMap implementation of the java.util.Map interface does not allow duplicate keys (it does allow the null key), distinct keys can map to duplicate values and it is neither ordered nor sorted. If you are looking for a sorted Map implementation you should have a look at java.util.TreeMap.
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.) Existing key-value pair will be replaced by new comer key-value pair, if keys are same... So, You will lose the previous value...

2.) Yes...

3.) Its neither sorted nor ordered... But implicitly it takes the key-value pair based on hashcode in ascending order... So you cant expect them in an order...
 
Komal Arora
Ranch Hand
Posts: 91
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lol yeah Jelle, one question led to another and in no time one became three!

Jelle and Ram, Thanks for the explanation
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic