• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference b/w Hashmap and linkedHashmap  RSS feed

 
anuj guptaa
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

As I was trying to understand the difference
Between the implementation of the hashmap(HM) and
Linked hashmap(LHM). I understand HM has Array and linked list for internal implementation

For LHM also the implementation is same as HM but it also maintain a double linked list to maintain the order of the list as per insertion or access

Now my question is does the LHM maintain an array , linkeslist and double linked list or
Array and double linked list

Thanks
Anuj
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anuj guptaa wrote:For LHM also the implementation is same as HM but it also maintain a double linked list to maintain the order of the list as per insertion or access

No, not the order of the "list"; but the order of entries.

Now my question is does the LHM maintain an array , linkeslist and double linked list or Array and double linked list

Probably the first (except that "linkeslist" will probably be "linked lists") - but does it matter?

The fact is that (normally) the entries returned by entrySet() will be in the order they were added to the Map. I believe that values() will also be in the order they were added, and keySet() will return keys in the order they were created; but I'd have to double-check to be absolutely sure.

Winston
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

LinkedHashMap extends HashMap and believe me inherits almost all methods except a few.
The difference between them is that LinkedHashMap maintains order of Entries whereas HashMap does not guarantee any order.
The cool thing is that LinkedHashMap offers two types of order to store mappings by:
1. Insert-order - the default one
2. Access-order

If you really want to know LinkedHashMap you need to know HashMap. For that please cover my Internal life of HashMap tutorial.
And only after that one you may cover Internal life Of LinkedHashMap tutorial.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!