aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HashMaps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HashMaps" Watch "HashMaps" New topic
Author

HashMaps

Komal Arora
Ranch Hand

Joined: Sep 30, 2010
Posts: 91

just a simple question: Do hashMaps allow duplicate keys? can there be distinct keys but duplicate values? is Hashmap sorted or ordered?


OCPJP
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1666
    
    7

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.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

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...


SCJP 6 [SCJP - Old is Gold]
Komal Arora
Ranch Hand

Joined: Sep 30, 2010
Posts: 91

Lol yeah Jelle, one question led to another and in no time one became three!

Jelle and Ram, Thanks for the explanation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMaps
 
Similar Threads
HashTable / HashMap..?
A dumb question
Memory size and access
sorting a array
duplicates in collection