user1993412
user1993412

Reputation: 862

Creating a sorted Set

Is there a way to sort a Set?

I have a Set of double and I am looking for a way to sort them.
I understand it is very easy using List, but something using Set is getting a bit tricky to me.

Any inputs would be great help.

Upvotes: 2

Views: 8382

Answers (3)

Ankur Lathi
Ankur Lathi

Reputation: 7836

Sorted set:

return new TreeSet(setIWantSorted);

Upvotes: 2

Bhavik Shah
Bhavik Shah

Reputation: 5183

There are 2 ways

  1. Use SortedSet
  2. Use TreeSet

SortedSet is an interface The detailed hierarchy is mentioned in the link

To use TreeSet Here's a link for an SO answer

Upvotes: 0

sanbhat
sanbhat

Reputation: 17622

You should put them into TreeSet. TreeSet is a SortedSet implementation which orders the elements according to the natural ordering.

TreeSet accepts objects which implements Comparable which defines the sort order. Double objects can be put into TreeSet since they implement Comparable

Upvotes: 10

Related Questions