The element to add must implement the Comparable interface or
the TreeSet constructor must have been passed a Comparator.
If both of these are not true
then a ClassCastException will be thrown.
import java.util.*;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
public class MainClass {
public static void main(String args[]) throws Exception {
String elements[] = { "A", "C", "D", "G", "F" };
Set set = new TreeSet(Collections.reverseOrder());
for (int i = 0, n = elements.length; i < n; i++) {
set.add(elements[i]);
}
System.out.println(set);
}
}
[G, F, D, C, A]