JAVA SortedMap接口

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(" ");
  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(" ");
  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(" ");
  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

原文地址:https://www.cnblogs.com/jpfss/p/9772818.html