前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java 中TreeMap实现详细代码

Java 中TreeMap实现详细代码

作者头像
用户7886150
修改2021-04-26 17:42:48
3180
修改2021-04-26 17:42:48
举报
文章被收录于专栏:bit哲学院bit哲学院

参考链接: Java TreeMap

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

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

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

HashMap与TreeMap的比较 

public class SortedMapTest2 {          public static void main(String[] args) {                  Map<String,Object> hashMap = new HashMap<String,Object>();         hashMap.put("1", "a");         hashMap.put("5", "b");         hashMap.put("2", "c");         hashMap.put("4", "d");         hashMap.put("3", "e");         Set<Entry<String, Object>> entry = hashMap.entrySet();         for(Entry<String, Object> temp : entry){             System.out.println("hashMap:"+temp.getKey()+" 值"+temp.getValue());           }                  System.out.println("\n");                    SortedMap<String,Object> sortedMap = new TreeMap<String,Object>();         sortedMap.put("1", "a");         sortedMap.put("5", "b");         sortedMap.put("2", "c");         sortedMap.put("4", "d");         sortedMap.put("3", "e");         Set<Entry<String, Object>> entry2 = sortedMap.entrySet();         for(Entry<String, Object> temp : entry2){             System.out.println("sortedMap:"+temp.getKey()+" 值"+temp.getValue());           }              }   } 运算的结果为 

hashMap:1 值a hashMap:2 值c hashMap:3 值e hashMap:4 值d hashMap:5 值b   sortedMap:1 值a sortedMap:2 值c sortedMap:3 值e sortedMap:4 值d sortedMap:5 值b 看上去还以为HashMap也保证了有序性,其实是随机的,如果值设置的复杂一点,如下例: 

public class SortedMapTest3 {          public static void main(String[] args) {                  Map<String,Object> hashMap = new HashMap<String,Object>();         hashMap.put("1b", "a");         hashMap.put("2", "b");         hashMap.put("4b", "d");         hashMap.put("3", "c");         hashMap.put("2b", "d");         hashMap.put("3b", "c");         Set<Entry<String, Object>> entry = hashMap.entrySet();         for(Entry<String, Object> temp : entry){             System.out.println("hashMap:"+temp.getKey()+" 值"+temp.getValue());           }                  System.out.println("\n");                    SortedMap<String,Object> sortedMap = new TreeMap<String,Object>();         sortedMap.put("1b", "a");         sortedMap.put("2", "b");         sortedMap.put("4b", "d");         sortedMap.put("3", "c");         sortedMap.put("2b", "d");         sortedMap.put("3b", "c");         Set<Entry<String, Object>> entry2 = sortedMap.entrySet();         for(Entry<String, Object> temp : entry2){             System.out.println("sortedMap:"+temp.getKey()+" 值"+temp.getValue());           }              }   } 运算的结果是: 

hashMap:2b 值d hashMap:1b 值a hashMap:2 值b hashMap:3 值c hashMap:4b 值d hashMap:3b 值c   sortedMap:1b 值a sortedMap:2 值b sortedMap:2b 值d sortedMap:3 值c sortedMap:3b 值c sortedMap:4b 值d 很显然只有TreeMap保证了有序性。 

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

public class SortedMapTest {          public static void main(String[] args) {                  SortedMap<String,String> sortedMap = new TreeMap<String,String>();         sortedMap.put("1", "a");         sortedMap.put("5", "b");         sortedMap.put("2", "c");         sortedMap.put("4", "d");         sortedMap.put("3", "e");         Set<Entry<String, String>> entry2 = sortedMap.entrySet();         for(Entry<String, String> temp : entry2){             System.out.println("修改前 :sortedMap:"+temp.getKey()+" 值"+temp.getValue());           }         System.out.println("\n");                    //这里将map.entrySet()转换成list         List<Map.Entry<String,String>> list =                  new ArrayList<Map.Entry<String,String>>(entry2);                  Collections.sort(list, new Comparator<Map.Entry<String,String>>(){               @Override             public int compare(Entry<String, String> o1, Entry<String, String> o2) {                 // TODO Auto-generated method stub                 return o1.getValue().compareTo(o2.getValue());             }                      });                  for(Map.Entry<String,String> temp :list){             System.out.println("修改后 :sortedMap:"+temp.getKey()+" 值"+temp.getValue());           }     }   } 运行结果为: 

修改前 :sortedMap:1 值a 修改前 :sortedMap:2 值c 修改前 :sortedMap:3 值e 修改前 :sortedMap:4 值d 修改前 :sortedMap:5 值b   修改后 :sortedMap:1 值a 修改后 :sortedMap:5 值b 修改后 :sortedMap:2 值c 修改后 :sortedMap:4 值d 修改后 :sortedMap:3 值e

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档