aspose file tools*
The moose likes Beginning Java and the fly likes Using a Comparator in Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using a Comparator in Collections" Watch "Using a Comparator in Collections" New topic
Author

Using a Comparator in Collections

shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147




I have a doubt in this code, where is the compare method called in the CompDemo class ??
The output is F E D C B A.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
From lines 19 through 24. Since you have specified a comparator, the compare() method is called when you add each element to the TreeSet.

Read -> http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html#TreeSet(java.util.Comparator)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
By compareTo() I presume you mean compare()?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Thanks for correcting that Campbell... i have edited my initial reply..
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using a Comparator in Collections