SortedMap接口主要提供有序的Map实现。

Map的主要实现有HashMap,TreeMap,HashTable,LinkedHashMap。

TreeMap实现了SortedMap接口,保证了有序性。默认的排序是根据key值进行升序排序,也可以重写comparator方法来根据value进行排序。

HashMap与TreeMap的比较


  1. public class SortedMapTest2 {
  2. public static void main(String[] args) {
  3. Map<String,Object> hashMap = new HashMap<String,Object>();
  4. hashMap.put("1", "a");
  5. hashMap.put("5", "b");
  6. hashMap.put("2", "c");
  7. hashMap.put("4", "d");
  8. hashMap.put("3", "e");
  9. Set<Entry<String, Object>> entry = hashMap.entrySet();
  10. for(Entry<String, Object> temp : entry){
  11. System.out.println("hashMap:"+temp.getKey()+" 值"+temp.getValue());
  12. }
  13. System.out.println("\n");
  14. SortedMap<String,Object> sortedMap = new TreeMap<String,Object>();
  15. sortedMap.put("1", "a");
  16. sortedMap.put("5", "b");
  17. sortedMap.put("2", "c");
  18. sortedMap.put("4", "d");
  19. sortedMap.put("3", "e");
  20. Set<Entry<String, Object>> entry2 = sortedMap.entrySet();
  21. for(Entry<String, Object> temp : entry2){
  22. System.out.println("sortedMap:"+temp.getKey()+" 值"+temp.getValue());
  23. }
  24. }
  25. }

运算的结果为


  1. hashMap:1 值a
  2. hashMap:2 值c
  3. hashMap:3 值e
  4. hashMap:4 值d
  5. hashMap:5 值b
  6. sortedMap:1 值a
  7. sortedMap:2 值c
  8. sortedMap:3 值e
  9. sortedMap:4 值d
  10. sortedMap:5 值b

看上去还以为HashMap也保证了有序性,其实是随机的,如果值设置的复杂一点,如下例:


  1. public class SortedMapTest3 {
  2. public static void main(String[] args) {
  3. Map<String,Object> hashMap = new HashMap<String,Object>();
  4. hashMap.put("1b", "a");
  5. hashMap.put("2", "b");
  6. hashMap.put("4b", "d");
  7. hashMap.put("3", "c");
  8. hashMap.put("2b", "d");
  9. hashMap.put("3b", "c");
  10. Set<Entry<String, Object>> entry = hashMap.entrySet();
  11. for(Entry<String, Object> temp : entry){
  12. System.out.println("hashMap:"+temp.getKey()+" 值"+temp.getValue());
  13. }
  14. System.out.println("\n");
  15. SortedMap<String,Object> sortedMap = new TreeMap<String,Object>();
  16. sortedMap.put("1b", "a");
  17. sortedMap.put("2", "b");
  18. sortedMap.put("4b", "d");
  19. sortedMap.put("3", "c");
  20. sortedMap.put("2b", "d");
  21. sortedMap.put("3b", "c");
  22. Set<Entry<String, Object>> entry2 = sortedMap.entrySet();
  23. for(Entry<String, Object> temp : entry2){
  24. System.out.println("sortedMap:"+temp.getKey()+" 值"+temp.getValue());
  25. }
  26. }
  27. }

运算的结果是:


  1. hashMap:2b 值d
  2. hashMap:1b 值a
  3. hashMap:2 值b
  4. hashMap:3 值c
  5. hashMap:4b 值d
  6. hashMap:3b 值c
  7. sortedMap:1b 值a
  8. sortedMap:2 值b
  9. sortedMap:2b 值d
  10. sortedMap:3 值c
  11. sortedMap:3b 值c
  12. sortedMap:4b 值d

很显然只有TreeMap保证了有序性。

那如果想要根据value值来进行排序


  1. public class SortedMapTest {
  2. public static void main(String[] args) {
  3. SortedMap<String,String> sortedMap = new TreeMap<String,String>();
  4. sortedMap.put("1", "a");
  5. sortedMap.put("5", "b");
  6. sortedMap.put("2", "c");
  7. sortedMap.put("4", "d");
  8. sortedMap.put("3", "e");
  9. Set<Entry<String, String>> entry2 = sortedMap.entrySet();
  10. for(Entry<String, String> temp : entry2){
  11. System.out.println("修改前 :sortedMap:"+temp.getKey()+" 值"+temp.getValue());
  12. }
  13. System.out.println("\n");
  14. //这里将map.entrySet()转换成list
  15. List<Map.Entry<String,String>> list =
  16. new ArrayList<Map.Entry<String,String>>(entry2);
  17. Collections.sort(list, new Comparator<Map.Entry<String,String>>(){
  18. @Override
  19. public int compare(Entry<String, String> o1, Entry<String, String> o2) {
  20. // TODO Auto-generated method stub
  21. return o1.getValue().compareTo(o2.getValue());
  22. }
  23. });
  24. for(Map.Entry<String,String> temp :list){
  25. System.out.println("修改后 :sortedMap:"+temp.getKey()+" 值"+temp.getValue());
  26. }
  27. }
  28. }

运行结果为:


  1. 修改前 :sortedMap:1 值a
  2. 修改前 :sortedMap:2 值c
  3. 修改前 :sortedMap:3 值e
  4. 修改前 :sortedMap:4 值d
  5. 修改前 :sortedMap:5 值b
  6. 修改后 :sortedMap:1 值a
  7. 修改后 :sortedMap:5 值b
  8. 修改后 :sortedMap:2 值c
  9. 修改后 :sortedMap:4 值d
  10. 修改后 :sortedMap:3 值e

JAVA SortedMap接口的更多相关文章

  1. Java 数据类型:集合接口Map:HashTable;HashMap;IdentityHashMap;LinkedHashMap;Properties类读取配置文件;SortedMap接口和TreeMap实现类:【线程安全的ConcurrentHashMap】

    Map集合java.util.Map Map用于保存具有映射关系的数据,因此Map集合里保存着两个值,一个是用于保存Map里的key,另外一组值用于保存Map里的value.key和value都可以是 ...

  2. SortedMap接口:进行排序操作。

    回顾:SortedSet是TreeSet的实现接口,此接口可以排序. SortedMap接口同样可以排序,是TreeMap的实现接口,父类. 定义如下: public class TreeMap< ...

  3. SortedMap接口的实现类TreeMap介绍和实现Comparator自定义比较器(转)

    与SortedSet接口类似,SortedMap也是一个结构,待排序的Map,其一个比较常用的实现类是TreeMap. TreeMap的put(K key, V value)方法在每添加一个元素时,都 ...

  4. SortedMap接口

    SortedMap接口是排序接口,只要是实现了此接口的子类,都属于排序的子类,TreeMap也是此接口的一个子类. import java.util.Map; import java.util.Sor ...

  5. 深入理解Java的接口和抽象类(转)

    深入理解Java的接口和抽象类 对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太多不同的地方.很多人在初学的 ...

  6. 深入理解Java的接口和抽象类

    深入理解Java的接口和抽象类 对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太多不同的地方.很多人在初学的 ...

  7. java微信接口之五—消息分组群发

    一.微信消息分组群发接口简介 1.请求:该请求是使用post提交地址为: https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_t ...

  8. java微信接口之四—上传素材

    一.微信上传素材接口简介 1.请求:该请求是使用post提交地址为: https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=A ...

  9. android 学习随笔二十七(JNI:Java Native Interface,JAVA原生接口 )

    JNI(Java Native Interface,JAVA原生接口) 使用JNI可以使Java代码和其他语言写的代码(如C/C++代码)进行交互. 问:为什么要进行交互? 首先,Java语言提供的类 ...

随机推荐

  1. RAC RMAN 备份 RMAN-03009 ORA-19504 ORA-27040 RMAN-06012 channel c3 not allocated 错误分析

    备份Shell 脚本如下: ######################################################################## ## RAC_hot_da ...

  2. Web自动化测试 Selenium+Eclipse+Junit+TestNG+Python

    Selenium+Eclipse+Junit+TestNG+Python 第三步 下载Selenium IDE.SeleniumRC.IEDriverServer.SeleniumClient Dri ...

  3. 从反编译深入理解JAVA内部类类结构以及finalkeyword

    1.为什么成员内部类能够无条件訪问外部类的成员? 在此之前,我们已经讨论过了成员内部类能够无条件訪问外部类的成员,那详细到底是怎样实现的呢?以下通过反编译字节码文件看看到底.其实,编译器在进行编译的时 ...

  4. mysql常见故障诊断

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010230971/article/details/80335578 作为故障预警,应该尽量把问题扼 ...

  5. 【Codeforces Round #440 (Div. 2) A】 Search for Pretty Integers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先枚举一个数字的情况. 再枚举两个数的情况就好. [代码] #include <bits/stdc++.h> #defi ...

  6. Spring Boot Freemarker特别篇之contextPath【从零开始学Spring Boot】(转)

    需求缘起:有人在群里@我:请教群主大神一个问题,spring boot  + freemarker 怎么获取contextPath 头疼死我了,网上没一个靠谱的 .我就看看之前博客中的 [Spring ...

  7. iOS开发 - Quartz2D画图

    Quartz 2D简单介绍 是一个二维画图引擎,同一时候支持iOS和Mac系统 Quartz 2D能完毕的工作 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片(图像) 读取\生成 ...

  8. JAVA初始开发环境搭建

    上午想在一台新电脑上搭建java开发环境,在没有之前备份的情况下,单靠网络还真有点麻烦.最主要的原因是貌似在我当前的网络环境下jdk无法下载,官网这个链接半天打不开,http://www.oracle ...

  9. IPv4与IPv6数据报格式详解

    摘要: 本文给出IPv4与IPv6数据报格式示意图,并整理了各个字段含义,最后对比IPv4与IPv6数据报格式的区别. 一.IPv4数据报 图1 IPv4数据报格式版本号(version) 不同的IP ...

  10. 【BZOJ 3675】[Apio2014]序列分割

    [链接] 链接 [题意] 在这里输入题意 [题解] 模拟一下样例. 会发现.切的顺序不影响最后的答案. 只要切点确定了. 答案就确定了. 则设f[i][j]表示前i段,第i段保留到j的最大值. \(f ...