【DateStructure】 Charnming usages of Map collection in Java

 When learning the usage of map collection in java, I found serveral beneficial methods that was encountered in the daily life. Now  I made a summary: 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. import java.util.ArrayList;  
  2. import java.util.Collections;  
  3. import java.util.Comparator;  
  4. import java.util.HashMap;  
  5. import java.util.Iterator;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8. import java.util.SortedMap;  
  9. import java.util.TreeMap;  
  10.   
  11. public class MapUtil  
  12. {  
  13.   
  14.     private static final Map<String, String> contents = new HashMap<String, String>();  
  15.     @SuppressWarnings("unchecked")  
  16.     public static void initMap()  
  17.     {  
  18.         Map testMap = new HashMap<String, String>();  
  19.         testMap.put("Albert""Shao");  
  20.         contents.putAll(testMap);  
  21.     }  
  22.   
  23.     /** 
  24.      * Four methods to list map. 
  25.      * output: 
  26.      * Albert:Shao 
  27.        Albert:Shao 
  28.        Shao 
  29.        Albert:Shao 
  30.         
  31.      * @time Jul 18, 2014 11:39:46 AM 
  32.      * @return void 
  33.      */  
  34.     public static void listMap()  
  35.     {  
  36.         Map<String, String> testMap = new HashMap<String, String>();  
  37.         testMap.put("Albert""Shao");  
  38.         for (Map.Entry<String, String> entry : testMap.entrySet())  
  39.         {  
  40.             System.out.println(entry.getKey() + ":" + entry.getValue());  
  41.         }  
  42.   
  43.         for (String key : testMap.keySet())  
  44.         {  
  45.             System.out.println(key + ":" + testMap.get(key));  
  46.         }  
  47.   
  48.         for (String value : testMap.values())  
  49.         {  
  50.             System.out.println(value);  
  51.         }  
  52.   
  53.         Iterator<Map.Entry<String, String>> keyIt = testMap.entrySet()  
  54.                 .iterator();  
  55.         while (keyIt.hasNext())  
  56.         {  
  57.             Map.Entry<String, String> entry = keyIt.next();  
  58.             System.out.println(entry.getKey() + ":" + entry.getValue());  
  59.         }  
  60.     }  
  61.   
  62.     /** 
  63.      * Use the treeMap order by key asc. 
  64.      * Watch out: if key is repeated, the latter element will replace the former. 
  65.      * output: {Apple=five, Banana=three, Grape=one, Pair=four}  
  66.      *  
  67.      * @time Jul 18, 2014 11:37:51 AM 
  68.      * @return void 
  69.      */  
  70.     public static void sort()  
  71.     {  
  72.         SortedMap<String, String> sortMap = new TreeMap<String, String>();  
  73.         sortMap.put("Pair""four");  
  74.         sortMap.put("Apple""two");  
  75.         sortMap.put("Grape""one");  
  76.         sortMap.put("Banana""three");  
  77.         sortMap.put("Apple""five");  
  78.         System.out.println(sortMap);  
  79.     }  
  80.   
  81.     /** 
  82.      * Sort the Map by map.value, then set the result to map. 
  83.      * output : [Apple=1, Pair=2, Banana=3, Grape=4] 
  84.      *  
  85.      * @time Jul 18, 2014 11:36:28 AM 
  86.      * @return void 
  87.      */  
  88.     public static void sortByValue()  
  89.     {  
  90.         Map<String, Integer> testMap = new HashMap<String, Integer>();  
  91.         testMap.put("Pair"2);  
  92.         testMap.put("Apple"1);  
  93.         testMap.put("Grape"4);  
  94.         testMap.put("Banana"3);  
  95.   
  96.         List<Map.Entry<String, Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>(  
  97.                 testMap.entrySet());  
  98.         Collections.sort(entryList,  
  99.                 new Comparator<Map.Entry<String, Integer>>()  
  100.                 {  
  101.                     public int compare(Map.Entry<String, Integer> c1,  
  102.                             Map.Entry<String, Integer> c2)  
  103.                     {  
  104.                         return (c1.getValue() - c2.getValue());  
  105.                     }  
  106.                 });  
  107.   
  108.         System.out.println(entryList);  
  109.     }  
  110.        
  111.     /** 
  112.      *Sort map by value when value is object.  
  113.      * use compareTo method to replace simple '-' 
  114.      * output:[Apple=AB, Grape=AF, Pair=BB, Banana=XY] 
  115.      *  
  116.      * @time Jul 18, 2014 11:48:35 AM 
  117.      * @return void 
  118.      */  
  119.     public static void sortByObject()  
  120.     {  
  121.         Map<String, String> testMap = new HashMap<String, String>();  
  122.         testMap.put("Pair""BB");  
  123.         testMap.put("Apple""AB");  
  124.         testMap.put("Grape""AF");  
  125.         testMap.put("Banana""XY");  
  126.   
  127.         List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(  
  128.                 testMap.entrySet());  
  129.         Collections.sort(entryList,  
  130.                 new Comparator<Map.Entry<String, String>>()  
  131.                 {  
  132.                     public int compare(Map.Entry<String, String> c1,  
  133.                             Map.Entry<String, String> c2)  
  134.                     {  
  135.                         return (c1.getValue().compareTo(c2.getValue()));  
  136.                     }  
  137.                 });  
  138.   
  139.         System.out.println(entryList);  
  140.     }  
  141.       
  142.     public static void main(String[] args)  
  143.     {  
  144.         MapUtil.listMap();  
  145.         MapUtil.sort();  
  146.         MapUtil.sortByValue();  
  147.         MapUtil.sortByObject();  
  148.     }  
  149. }  

If you want to further know about the usage of list methods, you could view my another blogs. 

http://blog.csdn.net/sxb0841901116/article/details/20635267

原文地址:https://www.cnblogs.com/lcchuguo/p/4170308.html