Java - The SortedMap Interface
2015-06-07 21:12
344 查看
The SortedMap interface extends Map. It ensures that the entries are maintained in ascending key order
Several methods throw a NoSuchElementException when no items are in the invoking map. A ClassCastException is thrown when an object is incompatible with the elements in a map. A NullPointerException is thrown if an attempt is made to use a null object when null is not allowed in the map.
The methods declared by SortedMap are summarized in the following table:
SN | Methods with Description |
---|---|
1 | Comparator comparator( ) Returns the invoking sorted map's comparator. If the natural ordering is used for the invoking map, null is returned. |
2 | Object firstKey( ) Returns the first key in the invoking map. |
3 | SortedMap headMap(Object end) Returns a sorted map for those map entries with keys that are less than end. |
4 | Object lastKey( ) Returns the last key in the invoking map. |
5 | SortedMap subMap(Object start, Object end) Returns a map containing those entries with keys that are greater than or equal to start and less than end |
6 | SortedMap tailMap(Object start) Returns a map containing those entries with keys that are greater than or equal to start. |
Example:
SortedMap have its implementation in various classes like TreeMap, Following is the example to explain SortedMap functionlaity:import java.util.*; public class TreeMapDemo { public static void main(String args[]) { // Create a hash map TreeMap tm = new TreeMap(); // Put elements to the map tm.put("Zara", new Double(3434.34)); tm.put("Mahnaz", new Double(123.22)); tm.put("Ayan", new Double(1378.00)); tm.put("Daisy", new Double(99.22)); tm.put("Qadir", new Double(-19.08)); // Get a set of the entries Set set = tm.entrySet(); // Get an iterator Iterator i = set.iterator(); // Display elements while(i.hasNext()) { Map.Entry me = (Map.Entry)i.next(); System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } System.out.println(); // Deposit 1000 into Zara's account double balance = ((Double)tm.get("Zara")).doubleValue(); tm.put("Zara", new Double(balance + 1000)); System.out.println("Zara's new balance: " + tm.get("Zara")); } }
This would produce the following result:
Ayan: 1378.0 Daisy 99.22 Mahnaz: 123.22 Qadir: -19.08 Zara: 3434.34 Zara.s current balance: 4434.34
相关文章推荐
- 006_03Java解析XML之DOM方式与SAX方式
- 在ubuntu中如何安装sun-java6-jdk
- java用正则表达式判断字符串中是否仅包含英文字母、数字和汉字
- spring实战学习笔记一2015年6月7号
- Java中的泛型方法
- java程序打包jar文件自带图片
- 老农解决猫狗鱼的问题(黑马基础习题)
- Eclipse下使用SVN插件从服务器获取工程
- java中使用正则表达式
- JAVA类的序列化
- java中的IO流(2)----读取文本数据
- javaweb 一个登陆界面
- java中的IO流(1)----读取二进制文件
- Java for LeetCode 207 Course Schedule【Medium】
- javaSWing程序命令行和IDE运行没有问题,打包成jar后直接双击运行部分功能不行
- 《Java从入门到精通》第五章学习笔记
- 226_尚学堂_高淇_java300集最全视频教程_【GOF23设计模式】_单例模式_应用场景_饿汉式_懒汉式
- wait()+notify实现java生产者-消费者
- zookeeper - java操作
- 在集群中Java 通过调用API操作HBase 0.98