描述
這個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 為空並且此映射使用自然排序,或者其比較器不允許空鍵,則拋出此異常。
IllegalArgumentException- 如果 fromKey 大於 toKey 則拋出此異常;或者如果這個Map本身有一個受限的範圍,並且 fromKey 或 toKey 位於範圍的邊界之外。
示例
下麵的例子展示了 java.util.TreeMap.subMap() 的用法
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 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}
相關用法
- Java java.util.TreeMap.size()用法及代碼示例
- Java java.util.TreeMap.navigableKeySet()用法及代碼示例
- Java java.util.TreeMap.comparator()用法及代碼示例
- Java java.util.TreeMap.remove()用法及代碼示例
- Java java.util.TreeMap.firstKey()用法及代碼示例
- Java java.util.TreeMap.ceilingEntry()用法及代碼示例
- Java java.util.TreeMap.floorEntry()用法及代碼示例
- Java java.util.TreeMap.lastKey()用法及代碼示例
- Java java.util.TreeMap.clear()用法及代碼示例
- Java java.util.TreeMap.put()用法及代碼示例
- Java java.util.TreeMap.keySet()用法及代碼示例
- Java java.util.TreeMap.putAll()用法及代碼示例
- Java java.util.TreeMap.floorKey()用法及代碼示例
- Java java.util.TreeMap.firstEntry()用法及代碼示例
- Java java.util.TreeMap.pollFirstEntry()用法及代碼示例
- Java java.util.TreeMap.entrySet()用法及代碼示例
- Java java.util.TreeMap.clone()用法及代碼示例
- Java java.util.TreeMap.descendingKeySet()用法及代碼示例
- Java java.util.TreeMap.values()用法及代碼示例
- Java java.util.TreeMap.ceilingKey()用法及代碼示例
注:本文由純淨天空篩選整理自 java.util.TreeMap.subMap() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。