Java TreeMap tailMap() 方法



描述

Java TreeMap tailMap(K fromKey) 方法用於返回此對映中鍵大於或等於 fromKey 的部分的檢視。返回的對映由此對映支援,因此返回的對映中的更改反映在此對映中,反之亦然。

宣告

以下是 java.util.TreeMap.tailMap() 方法的宣告。

public SortedMap<K,V> tailMap(K fromKey)

引數

fromKey − 這是返回對映中鍵的低端點(包含)。

返回值

方法呼叫返回此對映中鍵大於或等於 fromKey 的部分的檢視。

異常

  • ClassCastException − 如果 fromKey 與此對映的比較器不相容,則丟擲此異常。

  • NullPointerException − 如果 fromKey 為 null 且此對映使用自然排序,或者其比較器不允許 null 鍵,則丟擲此異常。

  • IllegalArgumentException − 如果此對映本身具有受限範圍,並且 fromKey 位於範圍之外,則丟擲此異常。

Java TreeMap tailMap(K fromKey,boolean inclusive) 方法

描述

tailMap(K fromKey,boolean inclusive) 方法用於返回此對映中鍵大於(或等於,如果 inclusive 為 true)fromKey 的部分的檢視。返回的對映由此對映支援,因此返回的對映中的更改反映在此對映中,反之亦然。

宣告

以下是 java.util.TreeMap.tailMap() 方法的宣告。

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

引數

  • fromKey − 這是返回對映中鍵的低端點。

  • inclusive − 如果低端點要包含在返回的檢視中,則為 true。

返回值

方法呼叫返回此對映中鍵大於(或等於,如果 inclusive 為 true)fromKey 的部分的檢視。

異常

  • ClassCastException − 如果 fromKey 與此對映的比較器不相容,則丟擲此異常。

  • NullPointerException − 如果 fromKey 為 null 且此對映使用自然排序,或者其比較器不允許 null 鍵,則丟擲此異常。

  • IllegalArgumentException − 如果此對映本身具有受限範圍,並且 fromKey 位於範圍之外,則丟擲此異常。

獲取 Integer,Integer 對的 TreeMap 的 TailMap 示例

以下示例演示了 Java TreeMap tailMap(K fromKey) 方法的使用,以獲取此對映中鍵大於或等於 fromKey 的部分的檢視。我們建立了兩個 Integer,Integer 對的 TreeMap 物件。然後將一些條目新增到第一個對映中,並使用 subMap() 從第一個對映中檢索和列印子對映。

package com.tutorialspoint;

import java.util.SortedMap;
import java.util.TreeMap;

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

      // creating maps 
      TreeMap<Integer, Integer> treemap = new TreeMap<>();
      SortedMap<Integer, Integer> treemapincl = new TreeMap<>();

      // populating tree map
      treemap.put(2, 2);
      treemap.put(1, 1);
      treemap.put(3, 3);
      treemap.put(6, 6);
      treemap.put(5, 5);      

      System.out.println("Getting tail map");
      treemapincl = treemap.tailMap(3);
      System.out.println("Tail map values: "+treemapincl);      
   }    
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Getting tail map
Tail map values: {3=3, 5=5, 6=6}

獲取 Integer,String 對的 TreeMap 的 TailMap 示例

以下示例演示了 Java TreeMap tailMap(K fromKey) 方法的使用,以獲取此對映中鍵大於或等於 fromKey 的部分的檢視。我們建立了兩個 Integer,String 對的 TreeMap 物件。然後將一些條目新增到第一個對映中,並使用 subMap() 從第一個對映中檢索和列印子對映。

package com.tutorialspoint;

import java.util.SortedMap;
import java.util.TreeMap;

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

      // creating maps 
      TreeMap<Integer, String> treemap = new TreeMap<>();
      SortedMap<Integer, String> treemapincl = new TreeMap<>();

      // 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(3);
      System.out.println("Tail map values: "+treemapincl);      
   }    
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

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

獲取 Integer,String 對的 TreeMap 的 TailMap 示例

以下示例演示了 Java TreeMap tailMap(K fromKey, boolean inclusive) 方法的使用,以獲取此對映中鍵大於(或等於,如果 inclusive 為 true)fromKey 的部分的檢視。我們建立了兩個 Integer,String 的 TreeMap 物件。然後將一些條目新增到第一個對映中,並使用 tailMap() 從第一個對映中檢索和列印 tailMap。

package com.tutorialspoint;

import java.util.NavigableMap;
import java.util.TreeMap;

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

      // creating maps 
      TreeMap<Integer, String> treemap = new TreeMap<>();
      NavigableMap<Integer, String> treemapincl = new TreeMap<>();

      // 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.htm
廣告

© . All rights reserved.