comparator()%uA0方法用于返回排序在映射上,或者为null键,如果此映射使用键的自然顺序进行比较。
声明
以下是java.util.TreeMap.comparator()方法的声明。
public Comparator<? super K> comparator()
参数
-
NA
返回值
方法调用返回用于排序在这个映射上,或者为null键,如果此映射使用键的自然顺序比较。
异常
-
NA
例子
下面的示例演示java.util.TreeMap.comparator()方法的用法。
package com.yiibai 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