Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a sorted Set

Tags:

java

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.

like image 884
user1993412 Avatar asked Sep 22 '26 11:09

user1993412


1 Answers

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

like image 129
sanbhat Avatar answered Sep 24 '26 01:09

sanbhat