當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java 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

相關用法


注:本文由純淨天空篩選整理自 java.util.TreeMap.comparator() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。