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( "\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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM