wood burning stoves 2.0
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
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: 2904

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


I agree. Here's the link: http://aspose.com/file-tools
subject: Collection
It's not a secret anymore!