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:
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; public class MapUtil { private static final Map<String, String> contents = new HashMap<String, String>(); @SuppressWarnings("unchecked") public static void initMap() { Map testMap = new HashMap<String, String>(); testMap.put("Albert", "Shao"); contents.putAll(testMap); } /** * Four methods to list map. * output: * Albert:Shao Albert:Shao Shao Albert:Shao * @time Jul 18, 2014 11:39:46 AM * @return void */ public static void listMap() { Map<String, String> testMap = new HashMap<String, String>(); testMap.put("Albert", "Shao"); for (Map.Entry<String, String> entry : testMap.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } for (String key : testMap.keySet()) { System.out.println(key + ":" + testMap.get(key)); } for (String value : testMap.values()) { System.out.println(value); } Iterator<Map.Entry<String, String>> keyIt = testMap.entrySet() .iterator(); while (keyIt.hasNext()) { Map.Entry<String, String> entry = keyIt.next(); System.out.println(entry.getKey() + ":" + entry.getValue()); } } /** * Use the treeMap order by key asc. * Watch out: if key is repeated, the latter element will replace the former. * output: {Apple=five, Banana=three, Grape=one, Pair=four} * * @time Jul 18, 2014 11:37:51 AM * @return void */ public static void sort() { SortedMap<String, String> sortMap = new TreeMap<String, String>(); sortMap.put("Pair", "four"); sortMap.put("Apple", "two"); sortMap.put("Grape", "one"); sortMap.put("Banana", "three"); sortMap.put("Apple", "five"); System.out.println(sortMap); } /** * Sort the Map by map.value, then set the result to map. * output : [Apple=1, Pair=2, Banana=3, Grape=4] * * @time Jul 18, 2014 11:36:28 AM * @return void */ public static void sortByValue() { Map<String, Integer> testMap = new HashMap<String, Integer>(); testMap.put("Pair", 2); testMap.put("Apple", 1); testMap.put("Grape", 4); testMap.put("Banana", 3); List<Map.Entry<String, Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>( testMap.entrySet()); Collections.sort(entryList, new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> c1, Map.Entry<String, Integer> c2) { return (c1.getValue() - c2.getValue()); } }); System.out.println(entryList); } /** *Sort map by value when value is object. * use compareTo method to replace simple '-' * output:[Apple=AB, Grape=AF, Pair=BB, Banana=XY] * * @time Jul 18, 2014 11:48:35 AM * @return void */ public static void sortByObject() { Map<String, String> testMap = new HashMap<String, String>(); testMap.put("Pair", "BB"); testMap.put("Apple", "AB"); testMap.put("Grape", "AF"); testMap.put("Banana", "XY"); List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>( testMap.entrySet()); Collections.sort(entryList, new Comparator<Map.Entry<String, String>>() { public int compare(Map.Entry<String, String> c1, Map.Entry<String, String> c2) { return (c1.getValue().compareTo(c2.getValue())); } }); System.out.println(entryList); } public static void main(String[] args) { MapUtil.listMap(); MapUtil.sort(); MapUtil.sortByValue(); MapUtil.sortByObject(); } }
http://blog.csdn.net/sxb0841901116/article/details/20635267
【DateStructure】 Charnming usages of Map collection in Java,布布扣,bubuko.com
【DateStructure】 Charnming usages of Map collection in Java
原文地址:http://blog.csdn.net/sxb0841901116/article/details/37927479