WAP to insert String object into TreeSet where elements are inserted in reverse alphabetical order

import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        TreeSet treeSet = new TreeSet(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                String s1 = o1.toString();
                String s2 = o2.toString();
                return s2.compareTo(s1);
            }
        });
        treeSet.add("The");
        treeSet.add("Code");
        treeSet.add("Data");
        treeSet.add("Java");
        treeSet.add("Program");
        System.out.println(treeSet);
    }
}
TreeSet

Similar Java Tutorials

Leave a Comment