Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

LinkedList,LinkedHashSet,LinkedHashMap

 
Satyajeet Kadam
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LinkedList,LinkedHashSet,LinkedHashMap are counter part of each other,I think they all have some simlarities.

Simlarites:
1) Insertion order is maintained
2) Elements are doubly linked with each other

In linkedList it mention that it is good choice for insertion and deletion as comapred to any collection in List Interface.
Q1) In LinkedHashSet, Can we say it is good choice for insertion and deletion as comapred to any collection in Map Interface?
Q2) In LnkedHashMap,addtion and deletion is slower as compared to HashMap? Why? I think it must be good choice for insertion
and deletion because elements are doubly linked with one another?






 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amolpalekar kadolkar wrote:Q1) In LinkedHashSet, Can we say it is good choice for insertion and deletion as comapred to any collection in Map Interface?

Um, why would we care about comparing to any Map interface? Maps and Sets do completely different things.

If you meant to ask if it was good or bad compared to other Set interfaces, it's a little slower than a plain HashSet, but pretty fast nonetheless.

amolpalekar kadolkar wrote:Q2) In LnkedHashMap,addtion and deletion is slower as compared to HashMap?


Yes - but it's only a little slower. Use the LinkedHashMap if you actually need the ordering it provides, and don't use it if you don't need it. But if you do need it, don't worry about the fact that it's slightly slower. It usually isn't noticeable.

amolpalekar kadolkar wrote:Why? I think it must be good choice for insertion and deletion because elements are doubly linked with one another?

Well, the hashtable algorithm already provides a very fast way to locate, add, or remove items. The fact that there are also bidirectional links between entries means that you need to spend a little more time setting up those links, in addition to setting up the hashtable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic