treeset comparator 28 29 method in java

Solutions on MaxInterview for treeset comparator 28 29 method in java by the best coders in the world

showing results for - "treeset comparator 28 29 method in java"
Nele
28 Nov 2016
1import java.util.TreeSet;
2public class TreeSetComparatorMethodExample
3{
4   public static void main(String[] args)
5   {
6      TreeSet<Integer> ts = new TreeSet<Integer>();
7      TreeSet<Integer> comp = new TreeSet<Integer>();
8      ts.add(5);
9      ts.add(6);
10      ts.add(7);
11      ts.add(8);
12      ts.add(9);
13      ts.add(10);
14      // use comparator
15      comp = (TreeSet)ts.comparator();
16      if(comp != null)
17      {
18         for(Integer element : comp)
19         {
20            System.out.println(element + " ");
21         }
22      }
23      else
24      {
25         System.out.println("TreeSet comparator value: " + comp);
26         System.out.println("Hence it is using natural ordering.");
27      }
28   }
29}