java.util.TreeMap.comparator() 方法
描述
comparator() 方法用于返回用于对该映射中的键进行排序的比较器,如果此映射使用其键的自然排序,则返回 null。
声明
以下是 java.util.TreeMap.comparator() 方法的声明。
public Comparator<? super K> comparator()
参数
NA
返回值
该方法调用返回用于对该映射中的键进行排序的比较器,如果此映射使用其键的自然排序,则返回 null。
异常
NA
示例
下面的例子展示了 java.util.TreeMap.comparator() 方法的使用。
package com.tutorialspoint;
import java.util.*;
public class TreeMapDemo {
public static void main(String[] args) {
// creating tree map
NavigableMap<Integer, String> treemap = new TreeMap<Integer, String>();
// populating tree map
treemap.put(2, "two");
treemap.put(1, "one");
treemap.put(3, "three");
treemap.put(6, "six");
treemap.put(5, "five");
// using comparator
Comparator comp = treemap.comparator();
System.out.println("Following natural ordering");
System.out.println("Comparator value: "+ comp);
}
}
让我们编译并运行上面的程序,这将产生以下结果.
Following natural ordering Comparator value: null

