map由键值对组成,所以排序类别有两种:
1.对键排序
2.对值排序
map的实现由两种:HashMap 和 TreeMap。对键的排序有点不同。对值的排序的一样。
TreeMap:
TreeMap会对键自动排序,默认为升序排列。要想自定义,需要重写比较器:Comparator<>()
Map<String,String> maps = new TreeMap<String,String>(
new Comparator<String>() {
public int compare(String s1,String s2) {
return s2.compareTo(s1);//逆序排列
}
});
maps.put("baa", "11");
maps.put("baa", "11");//不重复
maps.put("caa", "3");
maps.put("aaa", "41");
maps.put("eaa", "176");
maps.put("daa", "111");
maps.put("faa", "12");
System.out.println(maps);
结果:
{faa=12, eaa=176, daa=111, caa=3, baa=11, aaa=41}
对Value排序:
用list的Collections.sort(list, new Comparator<Map.Entrty<key,value>>(){
});进行排序。
先将Map元素存储到list中。
Map<String,Integer> map1 = new TreeMap<String,Integer>();
map1.put("21", 21);
map1.put("51", 51);
map1.put("31", 31);
map1.put("1", 1);
map1.put("41", 41);
map1.put("71", 71);
map1.put("61", 61);
//根据TreeMap的value来进行排序
List<Map.Entry<String, Integer>> lists = new ArrayList<Map.Entry<String,Integer>>(map1.entrySet());
用Collections进行compare()比较:
Collections.sort(lists,new Comparator<Map.Entry<String, Integer>>(){
public int compare(Map.Entry<String, Integer> m1,Map.Entry<String, Integer> m2) {
return m2.getValue().compareTo(m1.getValue());//降序
}
});
注意:比较的结果在List,而不是TreeMap。
对结果输出的时候遍历的是list。TreeMap仍然是按照键默认排序。
输出Treemap:
System.out.println();
for(Map.Entry<String, Integer> m0 : map1.entrySet()) {
System.out.println(m0.getKey() + "--" + m0.getValue());
}
结果:
1–1
21–21
31–31
41–41
51–51
61–61
71–71
输出list:
for(Map.Entry<String, Integer> m0 : lists) {
System.out.println(m0.getKey() + "--" + m0.getValue());
}
结果:
71–71
61–61
51–51
41–41
31–31
21–21
1–1
是按照Value的降序排列的。
HashMap:
HashMap不会对键自动排序。
自定义键排序可以将键全部取出利用数组排序。
Map<String,String> maps = new HashMap<>();
maps.put("aaa", "11");
maps.put("vaa", "3");
maps.put("6aa", "41");
maps.put("uaa", "176");
maps.put("qaa", "111");
maps.put("maa", "12");
Object[] keys = maps.keySet().toArray();
Arrays.sort(keys);
System.out.println("键值排序:");
for(Object key0 : keys) {
System.out.println(key0 + "--" + maps.get(key0));
}
结果:
6aa–41
aaa–11
maa–12
qaa–111
uaa–176
vaa–3
sort()默认为升序,可以使用
Arrays.sort(arr, Collections.reverseOrder());//降序排列
对Value排序:
与TreeMap一样:
利用Collections.sort(list, new Comparator<Map.Entry<>>(map.entry.set(){
});
Map<String,Integer> map1 = new TreeMap<String,Integer>();
map1.put("21", 21);
map1.put("51", 51);
map1.put("31", 31);
map1.put("1", 1);
map1.put("41", 41);
map1.put("71", 71);
map1.put("61", 61);
List<Map.Entry<String,Integer>> list = new ArrayList<Map.Entry<String,Integer>>(map1.entrySet());
Collections.sort(list,new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1,Map.Entry<String, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
//不一样
System.out.println("-map-");
for(Map.Entry<String, Integer> o0:map1.entrySet()) {
System.out.println(o0.getKey() + "--" + o0.getValue());
}
System.out.println("-list-");
for(Map.Entry<String, Integer> o0:list) {
System.out.println(o0.getKey() + "--" + o0.getValue());
}
结果:
-map-
1–1
21–21
31–31
41–41
51–51
61–61
71–71
-list-
71–71
61–61
51–51
41–41
31–31
21–21
1–1