headMap(K toKey)%uA0方法用于返回此映射的键严格小于toKey的部分视图。
声明
以下是java.util.TreeMap.headMap()方法的声明。
public SortedMap<K,V> headMap(K toKey)
参数
-
toKey--这是在返回映射中键的高端点(不包括)。
返回值
在方法调用返回此映射的键严格小于toKey部分视图。
异常
-
ClassCastException--%uA0抛出此异常如果toKey与此映射的比较兼容(如果该映射没有比较器,如果toKey没有实现Comparable)。实现方式可以,但不要求,抛出此异常,如果toKey不能键进行比较,在当前映射上。
-
NullPointerException--%uA0抛出此异常如果toKey为null,并且此映射使用自然排序,或者其比较器不允许使用null键。
-
IllegalArgumentException--%uA0这个异常被抛出,如果此映射本身有范围限制,并且toKey位于范围的边界之外。
例子
下面的示例演示java.util.TreeMap.headMap()方法的用法。
package com.yiibai 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> treemaphead = 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") // 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}