Answers for "How to sort treeset in descending order in java"

3

TreeSet descendingSet() method in java

import java.util.Iterator;
import java.util.NavigableSet;
import java.util.TreeSet;
public class TreeSetDescendingSetMethodExample
{
   public static void main(String[] args)
   {
      try
      {
         TreeSet<String> ts = new TreeSet<String>();
         ts.add("Apple");
         ts.add("Banana");
         ts.add("Cherry");
         ts.add("Dragon Fruit");
         System.out.println("TreeSet: " + ts);
         // reverse order view of element using descendingSet() method
         NavigableSet<String> ns = ts.descendingSet();
         Iterator<String> iterate = ns.iterator();
         System.out.println("Values using descendingSet() method: ");
         while(iterate.hasNext())
         {
            System.out.println(iterate.next());
         }
      }
      catch(NullPointerException ex)
      {
         System.out.println("Exception: " + ex);
      }
   }
}
Posted by: Guest on October-28-2020

Code answers related to "How to sort treeset in descending order in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language