This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between HashMap and LinkedHashMap

 
Robert Darling
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Robert Darling
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic