Java TreeMap subMap() 方法



描述

Java TreeMap subMap(K fromKey,K toKey) 方法用於返回此對映的一部分的檢視,其鍵的範圍從 fromKey(包含)到 toKey(不包含)。(如果 fromKey 和 toKey 相等,則返回的對映為空。)返回的對映由此對映支援,因此返回的對映中的更改會反映在此對映中,反之亦然。

宣告

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

public SortedMap<K,V> subMap(K fromKey,K toKey)

引數

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

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

返回值

方法呼叫返回此對映的一部分的檢視,其鍵的範圍從 fromKey(包含)到 toKey(不包含)。

異常

  • ClassCastException − 如果無法使用此對映的比較器將 fromKey 和 toKey 相互比較,則丟擲此異常。

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

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

Java TreeMap subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) 方法

描述

subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) 方法用於返回此對映的一部分的檢視,其鍵的範圍從 fromKey 到 toKey。如果 fromKey 和 toKey 相等,則返回的對映為空,除非 fromExclusive 和 toExclusive 都為 true。返回的對映由此對映支援,因此返回的對映中的更改會反映在此對映中,反之亦然。

宣告

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

public NavigableMap<K,V> subMap(K fromKey,
                                boolean fromInclusive,
                                K toKey,
                                boolean toInclusive)

引數

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

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

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

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

返回值

方法呼叫返回此對映的一部分的檢視,其鍵的範圍從 fromKey 到 toKey。

異常

  • ClassCastException − 如果無法使用此對映的比較器將 fromKey 和 toKey 相互比較,則丟擲此異常。

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

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

Integer,Integer 對的 TreeMap 的子對映示例

以下示例演示瞭如何使用 Java TreeMap subMap(K fromKey, K toKey) 方法獲取此對映的子對映,其鍵的範圍從 fromKey(包含)到 toKey(不包含)。我們建立了兩個 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 a portion of the map");
      treemapincl = treemap.subMap(1,5);
      System.out.println("Sub map values: "+treemapincl);      
   }    
}

輸出

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

Getting a portion of the map
Sub map values: {1=1, 2=2, 3=3}

Integer,String 對的 TreeMap 的子對映示例

以下示例演示瞭如何使用 Java TreeMap subMap(K fromKey, K toKey) 方法獲取此對映的子對映,其鍵的範圍從 fromKey(包含)到 toKey(不包含)。我們建立了兩個 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 a portion of the map");
      treemapincl = treemap.subMap(1,5);
      System.out.println("Sub map values: "+treemapincl);      
   }    
}

輸出

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

Getting a portion of the map
Sub map values: {1=one, 2=two, 3=three}

Integer,Object 對的 TreeMap 的子對映示例

以下示例演示瞭如何使用 Java TreeMap subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) 方法獲取此對映的子對映,其鍵的範圍從 fromKey 到 toKey,並對兩個鍵都使用包含標誌。我們建立了兩個 Integer,String 對的 TreeMap 物件。然後將一些條目新增到第一個對映中,並使用 subMap() 從第一個對映中檢索和列印子對映。

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 a portion of the map");
      treemapincl = treemap.subMap(1, true, 3, true);
      System.out.println("Sub map values: "+treemapincl);      
   }    
}

輸出

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

Getting a portion of the map
Sub map values: {1=one, 2=two, 3=three}
java_util_treemap.htm
廣告
© . All rights reserved.