Login with  Log in with facebook
Hiring Manager? SIGN UP HERE

How to sort a java.util.Set in Java?

Rishi Yadav
10/25/2011 at 17:58
3 Answers

There are two ways to sort a Set in Java class. 1) Using comparator interface 2) Using comparable interface.

Avani Shah
10/25/2011 at 18:05


Simply passing the set to the TreeSet constructor provided the objects are comparable.

Or using Collections.sort() method

Ankit Dhiman
01/14/2012 at 23:54


There many ways in whcih this can be done.

One can always convert it into an array using the toArray method and sort the array using the Arrays.sort method by providing the appropriate Comparator according to the ordering criteira.

One can also create instance of ArrayList from Set and then sort the List using the Collections.sort method.


Pravin Jain
01/25/2012 at 21:23

If you want to post any answer to this forum then you need to log in.
Schedule a Demo

Schedule a Demo with us

Name *
Email *
Phone *
Company *