jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes LinkedList,LinkedHashSet,LinkedHashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "LinkedList,LinkedHashSet,LinkedHashMap" Watch "LinkedList,LinkedHashSet,LinkedHashMap" New topic
Author

LinkedList,LinkedHashSet,LinkedHashMap

Satyajeet Kadam
Ranch Hand

Joined: Oct 19, 2006
Posts: 215
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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: LinkedList,LinkedHashSet,LinkedHashMap