This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collection" Watch "Collection" New topic


jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
Whats difference between

1. HashSet and LinkedHashSet. When do we used HashSet and when do we use LinkedHashSet ?
2. SortedSet and Tree Set. When do we use SortedSet and TreeSet ?
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902

1. LinkedHashSet keeps the order of element.
2. SortedSet is an interface and TreeSet is a concrete class implementing the SortedSet.

And the JavaDocs itself explains it beautifully.

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Tukaram Ugile

Joined: Mar 03, 2010
Posts: 4
HashSet is used when we want to get the unique elements by random order.
When we want these elements to be retrieved in the insertion order we can go for LinkedHashSet.

We use the TreeSet when we want the unique elements to be sorted in specific order.
Rajeev Trikha
Ranch Hand

Joined: Jan 29, 2010
Posts: 85
Jose also focus on NavigableSet which is a far more versatile interface than SortedSet. The subSet(from,in,,to,in) syntax and other variants like tailSet, headSet which provide inclusive boolean in their argument produce NavigableSet. The older type syntax of subSet(from,to) produce SortedSet. This is true of their Map equivalent too.

Rajeev Trikha (SCJP 6)
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
Hi Jose,

Just a suggestion

I think it will be great , if you share your understanding about collections and then we can have a healthy discussion about collection. It will help everyone.
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449
Consider Paul's rocket mass heater.
subject: Collection
Similar Threads
collections addAll method !!!!
Q on collections framework: iteration order
K&B Book Error?
Doubt in HashMap