Answers for "create comparator for treeset in java"

1

TreeSet comparator() method in java

import java.util.TreeSet;
public class TreeSetComparatorMethodExample
{
   public static void main(String[] args)
   {
      TreeSet<Integer> ts = new TreeSet<Integer>();
      TreeSet<Integer> comp = new TreeSet<Integer>();
      ts.add(5);
      ts.add(6);
      ts.add(7);
      ts.add(8);
      ts.add(9);
      ts.add(10);
      // use comparator
      comp = (TreeSet)ts.comparator();
      if(comp != null)
      {
         for(Integer element : comp)
         {
            System.out.println(element + " ");
         }
      }
      else
      {
         System.out.println("TreeSet comparator value: " + comp);
         System.out.println("Hence it is using natural ordering.");
      }
   }
}
Posted by: Guest on October-27-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language