java.util.TreeMap.headMap()方法实例

来源:互联网 发布:tower与其他软件 编辑:程序博客网 时间:2024/05/02 21:29

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.util.TreeMap.headMap()方法的用法。
import java.util.SortedMap;import java.util.TreeMap;public class TreeMapDemo {public static void main(String[] args) {TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();SortedMap<Integer, String> treemaphead = new TreeMap<Integer, String>();treemap.put(2, "two");treemap.put(1, "one");treemap.put(3, "three");treemap.put(6, "six");treemap.put(5, "five");treemaphead = treemap.headMap(4);System.out.println("checking value of the sorted map");System.out.println("Value is :"+treemaphead );}}

现在编译和运行上面的代码示例,将产生以下结果。
checking value of the sorted mapValue is :{1=one, 2=two, 3=three}

转自】http://www.yiibai.com/java/util/treemap_headmap.html

0 0
原创粉丝点击