java.util.TreeSet.comparator() 方法示例


java.util.TreeSet.comparator() 方法示例

package com.codingdict;

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetDemo {
   public static void main(String[] args) {

      // creating TreeSet
      TreeSet <Integer>tree = new TreeSet<Integer>();
      TreeSet <Integer>treecomp = new TreeSet<Integer>();

      // adding in the tree
      tree.add(12);
      tree.add(13);
      tree.add(14);
      tree.add(15);
      tree.add(16);
      tree.add(17);

      // using comparator
      treecomp = (TreeSet)tree.comparator();

      if(treecomp!=null) {
         for (Integer element : treecomp)
         System.out.println(element + " ");
      } else {
         System.out.println("treecomp value: "+treecomp);
         System.out.println("So it is using natural ordering");
      }
   }    
}