Java TreeMap headMap() 方法



描述

Java TreeMap headMap(K toKey) 方法用於返回此對映的一部分的檢視,其鍵嚴格小於 toKey。

宣告

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

public SortedMap<K,V> headMap(K toKey)

引數

toKey − 這是返回對映中鍵的高階點(不包括)。

返回值

方法呼叫返回此對映的一部分的檢視,其鍵嚴格小於 toKey。

異常

  • ClassCastException − 如果 toKey 與此對映的比較器不相容(或者,如果對映沒有比較器,如果 toKey 未實現 Comparable),則丟擲此異常。實現可以但不要求在 toKey 無法與對映中當前的鍵進行比較時丟擲此異常。

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

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

Java TreeMap headMap(K toKey,boolean inclusive) 方法

描述

headMap(K toKey,boolean inclusive) 方法用於返回此對映的一部分的檢視,其鍵小於(如果 inclusive 為 true,則小於或等於)toKey。

宣告

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

public NavigableMap<K,V> headMap(K toKey,boolean inclusive)

引數

  • toKey − 這是返回對映中鍵的高階點。

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

返回值

方法呼叫返回此對映的一部分的檢視,其鍵小於(如果 inclusive 為 true,則小於或等於)toKey。

異常

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

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

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

從 Integer、Integer 對的 TreeMap 獲取基於給定鍵的頭部對映示例

以下示例演示瞭如何使用 Java TreeMap headMap(K key) 方法獲取此對映的一部分的檢視,其鍵嚴格小於給定鍵。我們建立了 Integer、Integer 對的 TreeMap 和 SortedMap 物件。然後將一些條目新增到 treemap 物件中,並使用 headMap() 獲取給定鍵的檢視並列印它。

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> treemaphead = 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);

      // getting head map
      treemaphead = treemap.headMap(3);

      System.out.println("Checking values of the sorted map");
      System.out.println("Value is: "+ treemaphead);
   }    
}

輸出

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

Checking values of the sorted map
Value is: {1=1, 2=2}

從 Integer、String 對的 TreeMap 獲取基於給定鍵的頭部對映示例

以下示例演示瞭如何使用 Java TreeMap headMap(K key) 方法獲取此對映的一部分的檢視,其鍵嚴格小於給定鍵。我們建立了 Integer、String 對的 TreeMap 和 SortedMap 物件。然後將一些條目新增到 treemap 物件中,並使用 headMap() 獲取給定鍵的檢視並列印它。

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> treemaphead = 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");

      // getting head map
      treemaphead = treemap.headMap(3);

      System.out.println("Checking values of the sorted map");
      System.out.println("Value is: "+ treemaphead);
   }    
}

輸出

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

Checking values of the sorted map
Value is: {1=one, 2=two}

從 Integer、Object 對的 TreeMap 獲取基於給定鍵的頭部對映示例

以下示例演示瞭如何使用 Java TreeMap headMap(K key, boolean inclusive) 方法獲取此對映的一部分的檢視,其鍵嚴格小於給定鍵,同時包括高階點。我們建立了 Integer、Student 對的 TreeMap 和 SortedMap 物件。然後將一些條目新增到 treemap 物件中,並使用 headMap() 獲取給定鍵的檢視並列印它。

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> treemapheadincl = 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");

      // getting head map inclusive 3
      treemapheadincl = treemap.headMap(3,true);

      System.out.println("Checking values of the map");
      System.out.println("Value is: "+ treemapheadincl);
   }    
}

輸出

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

Checking values of the map
Value is: {1=one, 2=two, 3=three}
java_util_treemap.htm
廣告