| 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
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
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: 16483
|
|
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.
|
 |
 |
|
|
subject: Difference between HashMap and LinkedHashMap
|
|
|