SortedMap接口扩展了Map。它确保 元素以升序排列。
下表总结了SortedMap声明的方法-
链接:https://www.learnfk.comhttps://www.learnfk.com/java/java-sortedmap-interface.html
来源:LearnFk无涯教程网
Sr.No. | Method & Remark |
---|---|
1 | Comparator comparator() 返回调用排序的Map的比较器。如果自然顺序用于调用Map,则返回null。 |
2 | Object firstKey() 返回调用Map中的第一个键。 |
3 | SortedMap headMap(Objevt end) 返回键小于end的那些Map 元素的排序Map。 |
4 | Object lastKey() 返回调用Map中的最后一个键。 |
5 | SortedMap subMap(Object start,Object end) 返回包含与是大于或等于开始且小于端键的那些 元素的Map。 |
6 | SortedMap tailMap(Object start) 返回一个Map,其中包含键大于或等于start的那些 元素。 |
SortedMap在诸如TreeMap的各种类中都有其实现。以下是解释SortedMap功能的示例-
import java.util.*; public class TreeMapDemo { public static void main(String args[]) { //创建哈希映射 TreeMap tm=new TreeMap(); //将元素放入映射 tm.put("Learnfk", 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)); //获取一组集合 Set set=tm.entrySet(); //获取迭代器 Iterator i=set.iterator(); //显示元素 while(i.hasNext()) { Map.Entry me=(Map.Entry)i.next(); System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } System.out.println(); //将 1000 存入 Learnfk 的账户 double balance=((Double)tm.get("Learnfk")).doubleValue(); tm.put("Learnfk", new Double(balance + 1000)); System.out.println("Learnfk's new balance: " + tm.get("Learnfk")); } }
这将产生以下输出-
Ayan: 1378.0 Daisy: 99.22 Mahnaz: 123.22 Qadir: -19.08 Learnfk: 3434.34 Learnfk's new balance: 4434.34
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)