本文共 3210 字,大约阅读时间需要 10 分钟。
Map的排序常分为两种情况,1、按key值排序;2、按value排序
默认的情况下,TreeMap:是按key升序,进行排序的;LinkedHashMap:是按加入顺序进行排序的;HashMap:内部数值的顺序并不是以存放的先后顺序为主,而是以hash值的顺序为主,其次才是存放的先后顺序。
1.我们先讨论按key值进行排序 我们先看一下这个Sorter类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class Sorter { public static Map sort(Map map) { Map<Object, Object> mapVK = new TreeMap<Object, Object>( new Comparator<Object>() { public int compare(Object obj1, Object obj2) { String v1 = (String)obj1; String v2 = (String)obj2; int s = v2.compareTo(v1); return s; } } ); Set col = map.keySet(); Iterator iter = col.iterator(); while (iter.hasNext()) { String key = (String) iter.next(); Integer value = (Integer) map.get(key); mapVK.put(key, value); } return mapVK; } } |
最后给出一个例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | public class SortHashMap { public SortHashMap() { } public static void main(String[] args) { Map<String, Integer> maps = new HashMap<String, Integer>(); maps.put( "boy" , 8 ); maps.put( "cat" , 7 ); maps.put( "dog" , 1 ); maps.put( "apple" , 5 ); //排序前的输出 Set set = maps.entrySet(); Iterator i = set.iterator(); while (i.hasNext()){ Map.Entry<String, Integer> entry1=(Map.Entry<String, Integer>)i.next(); System.out.println(entry1.getKey() + "-------->" + entry1.getValue()); } System.out.println( "----------------" ); //排序后的输出 Map<String, Integer> sortMaps = Sorter.sort(maps); Set sortSet = sortMaps.entrySet(); Iterator ii = sortSet.iterator(); while (ii.hasNext()){ Map.Entry<String, Integer> entry1=(Map.Entry<String, Integer>)ii.next(); System.out.println(entry1.getKey() + "-------->" + entry1.getValue()); } } } |
排序前的输出结果是: cat-------->7 apple-------->5 dog-------->1 boy-------->8
排序后的输出结果是: dog-------->1 cat-------->7 boy-------->8 apple-------->5 经过排序后的Map有序了,是按照字母的逆序排列的。
2、我们再讨论如何按value值进行排序。 还是上面的那个例子,我们想要按照各类对象的数量打印出类别的名称。 我们再来修改一下这个SortHashMap类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public class SortHashMap { public SortHashMap() { } public static void main(String[] args) { Map<String, Integer> maps = new HashMap<String, Integer>(); maps.put( "boy" , 8 ); maps.put( "cat" , 7 ); maps.put( "dog" , 1 ); maps.put( "apple" , 5 ); //排序前的输出 Set set = maps.entrySet(); Iterator i = set.iterator(); while (i.hasNext()){ Map.Entry<String, Integer> entry1=(Map.Entry<String, Integer>)i.next(); System.out.println(entry1.getKey() + "-------->" + entry1.getValue()); } System.out.println( "----------------" ); //排序后的输出 List<Map.Entry<String, Integer>> info = new ArrayList<Map.Entry<String, Integer>>(maps.entrySet()); Collections.sort(info, new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> obj1, Map.Entry<String, Integer> obj2) { return obj2.getValue() - obj1.getValue(); } }); for ( int j = 0 ; j<info.size();j++) { System.out.println(info.get(j).getKey() + "------->" + info.get(j).getValue()); } } } |
排序前的输出结果是: cat-------->7 apple-------->5 dog-------->1 boy-------->8
排序后的输出结果是: boy------->8 cat------->7 apple------->5 dog------->1
程序运行的结果,达到了我们的要求,实现了Map的排序。该方法主要是利用了ArrayList的排序实现了Map的排序输出,并没有影响到Map的存放结构。