I used a Comparator class to define the sorting of the StringBuffer.I have implemented the Comparator class and the Comparator method compare.
Why i am getting output like this?
Code:
import java.util.Comparator;
import java.util.TreeSet;
public class SortestSetDemo {
public static void main(String[] args) {
TreeSet t1 = new TreeSet(new MyComparator());
t1.add(new StringBuffer("a"));
// t1.add("d");
t1.add(new StringBuffer("q"));
t1.add(new StringBuffer("w"));
t1.add(new StringBuffer("r"));
System.out.println(t1);
}
}
class MyComparator implements Comparator {
public int compare(Object ob1, Object ob2) {
// String i1=(String)ob1;
String i1 = ob1.toString();
// String i2=(String)ob2;
String i2 = ob2.toString(); //corrected error here instead of ob1.toString it is ob2.toString()
return -i1.compareTo(i2);
}
}
Output Shown: [a] instead of [a,q,r,w]
You have a typo in your code.
String i2 = ob1.toString();
. This should beBelow code is working fine -
Output: