Reputation: 862
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
Reputation: 5183
There are 2 ways
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
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