java.util.TreeMap.headMap()方法实例
2016-11-14 13:59
253 查看
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()方法的用法。
现在编译和运行上面的代码示例,将产生以下结果。
【转自】http://www.yiibai.com/java/util/treemap_headmap.html
声明
以下是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 map Value is :{1=one, 2=two, 3=three}
【转自】http://www.yiibai.com/java/util/treemap_headmap.html
相关文章推荐
- java.util.Collections.unmodifiableMap()方法实例
- java.util.Collections.synchronizedMap()方法实例
- java.util.Properties.getProperty()方法实例
- java.util.Collections.synchronizedSet()方法实例
- 生成随机数方法:java.util.Random.nextInt(int n)方法实例
- Java.util.Calendar.add()方法实例
- java.util.Calendar.getTime()方法实例
- java中Map集合的常用遍历方法及HashMap的应用实例
- java.util.Collections.max()方法实例
- java中Map集合的常用遍历方法及HashMap的应用实例
- java.util.Collections.unmodifiableList()方法实例
- java.util.Date.getTime()方法实例
- <Head First Java>学习笔记--第四章:方法操作实例变量 对象的行为
- java.util.Properties.getProperty()方法实例
- java.util.ArrayList.toArray(T[]) 方法实例
- java.util.ArrayList.add(int index, E elemen)方法实例
- java.util.Random.nextInt(int n)方法实例
- java.util.IdentityHashMap.entrySet()方法实例
- java.util.HashMap.remove()方法实例
- java.util.Collections.reverseOrder()方法实例