13
 Set<Student> ts = new TreeSet<Student>();

    for(Student s : studentInfo){
         ts.add(s);
    }

    System.out.println(ts);

I've written this above snippet in one of my case block in order to sort a collection of Student Objects. My question is: What is the difference between using this approach and using Collections.sort(); method.

Adam Arold
  • 29,285
  • 22
  • 112
  • 207
Anjan Baradwaj
  • 1,219
  • 5
  • 27
  • 54
  • 1
    Collection.sort() , can be optimised with a comparator method , which will sort the collection object according to the attribute you define , you can chose any attribute to be the decider one and create a comparing class defining the comparing method with that attribute – Hussain Akhtar Wahid 'Ghouri' Sep 12 '13 at 10:02

2 Answers2

13

The difference is that a TreeSet keeps you data sorted at all times while the Collections.sort() method sorts it when you call the method on your Set.

The time complexity of Collections.sort() is O(n*log(n)) while the TreeSet's add()'s complexity is log(n). If you use the same size of data then the complexity in the TreeSet's case will be the same because you repeat the add operation n times.

So you only have to decide whether you want your Set ordered at all times or just at some point. If there is a case in your code when you don't need sorting then you don't need TreeSet but if you will always need it to be sorted then you should use TreeSet.

Please keep in mind that if you want to sort your Set you have to create a List from it first which might introduce some overhead!

Another caveat: As others mentioned TreeSet can only take 1 Comparator while you can supply different Comparators to Collections.sort(). So it depends on your usage. You should give us more information on your use case in order to give you a thorough answer.

Adam Arold
  • 29,285
  • 22
  • 112
  • 207
  • I was editing my answer when your comment arrived. It is in there now. – Adam Arold Sep 12 '13 at 09:49
  • There are other cases in my code where i have add/search/display functionality. – Anjan Baradwaj Sep 12 '13 at 09:51
  • @AdamArold I have created studentInfo is a `LinkedList`, to which i add the Student objects in the first case. I've used the `TreeSet` to sort the same collection in a second case block. – Anjan Baradwaj Sep 12 '13 at 09:55
  • You should just use `Collections.sort()` then. – Adam Arold Sep 12 '13 at 09:56
  • BTW A set will drop duplicates (as defined by the comparator), whereas a List allows any number of duplicates. – Peter Lawrey Sep 12 '13 at 10:18
  • @AnjanBaradwaj An ArrayList is likely to be a better choice if you want to sort it. In most cases ArrayList is preferable to LinkedList. – Peter Lawrey Sep 12 '13 at 10:19
  • @PeterLawrey I had asked a question regarding that just yesterday. Here it is http://stackoverflow.com/questions/18734705/which-one-runs-faster-arraylist-or-linkedlist – Anjan Baradwaj Sep 12 '13 at 10:29
  • 1
    @AnjanBaradwaj ArrayList has a higher worst case add time but is many times faster on average. It also uses far less memory (as little as 1/4) Its references are continuous in memory making random but also sequential access more efficient. – Peter Lawrey Sep 12 '13 at 11:03
7

1) A TreeSet like all Set refuses duplicate values.

2) A TreeSet maintains the sort every time you insert elements while a list sorted with Collections.sort() will only be sorted after the call to sort() (and will not maintain this sort upon add()).

3) Collections.sort() allows to sort the list on differents criterias using different Comparators. With a TreeSet, you can also give a Comparator but you will need to instantiate one TreeSet for each Comparator.

Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148