This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between HashMap and LinkedHashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between HashMap and LinkedHashMap" Watch "Difference between HashMap and LinkedHashMap" New topic
Author

Difference between HashMap and LinkedHashMap

Robert Darling
Ranch Hand

Joined: Jun 21, 2008
Posts: 100

Hi,

While studying Java Collections , i cannot understand the main difference between HashMap and LinkedHashMap ? From what i understand The below given codes, HashMap does not maintain insertion order, but LinkedHashMap does ? Is this the only difference.

Output b,a,c


Output : a,b,c

Secondly how do you iterate over the values in a HashMap and HashTable ? Are we supposed to reuse the key returned by the keySet to get the value of the key ? Secondly a Set does not allow duplicates, so what about duplicate keys ?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

A map doesn't allow duplicate keys.
Yes you need to use the keySet's key to iterate over a map.
You can find the details of both HashMap and LinkedHashMap in the javadocs to find their differences, you already pointed out the main difference between the two...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Robert Darling
Ranch Hand

Joined: Jun 21, 2008
Posts: 100

Hi .. Thanks for the reply. You said that a MAP does not allow duplicate keys, but i understand a Hashtable does ? Is this correct. Both implement the MAP interface ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Sajjad Dar wrote:You said that a MAP does not allow duplicate keys, but i understand a Hashtable does ? Is this correct. Both implement the MAP interface ?


If it is correct, then the documentation would mention it. The documentation would also mention what interfaces it implements. So how about if you have a look there? That's what the docs are for, after all. Let us know what you find out.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Difference between HashMap and LinkedHashMap
 
Similar Threads
How to get data from Set in one go
same key for diff values in HashMap
Please Help: Putting Vector objects into HashMap
map object as a key and as a value
Map.Entry