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


Java java.util.TreeMap.tailMap()用法及代碼示例


描述

這個tailMap(K fromKey,boolean inclusive)方法用於返回此映射的部分的視圖,其鍵大於(或等於,如果 inclusive 為真)fromKey。返回的Map由此Map支持,因此返回的Map中的更改會反映在此Map中,反之亦然。

聲明

以下是聲明java.util.TreeMap.tailMap()方法。

public NavigableMap<K,V> tailMap(K fromKey,boolean inclusive)

參數

  • fromKey- 這是返回映射中鍵的低端點。

  • inclusive- 如果低端點要包含在返回的視圖中,則為真。

返回值

該方法調用返回此映射部分的視圖,其鍵大於(或等於,如果 inclusive 為真)fromKey。

異常

  • ClassCastException- 如果 fromKey 與此映射的比較器不兼容,則拋出此異常。

  • NullPointerException- 如果 fromKey 為 null 並且此映射使用自然排序,或者其比較器不允許空鍵,則拋出此異常。

  • IllegalArgumentException- 如果此Map本身具有受限範圍,並且 fromKey 位於範圍邊界之外,則會引發此異常。

示例

下麵的例子展示了 java.util.TreeMap.tailMap() 的用法

package com.tutorialspoint;

import java.util.*;

public class TreeMapDemo {
   public static void main(String[] args) {
      // creating maps 
      TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();
      SortedMap<Integer, String> treemapincl = 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");      
      
      System.out.println("Getting tail map");
      treemapincl = treemap.tailMap(2,true);
      System.out.println("Tail map values:"+treemapincl);      
   }    
}

讓我們編譯並運行上麵的程序,這將產生以下結果。

Getting tail map
Tail map values:{2=two, 3=three, 5=five, 6=six}

相關用法


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