一、未指定排序方式时
HashMap通过计算存入键值对的Key值进行排序
HashTree通过对存入的键值对的Key值进行升序排序
public static void main(String[] args){
Map<String,Integer> map = new HashMap<String, Integer>();
System.out.println("hashmap输出");
mapadd(map);
Map<String,Integer> map2 = new TreeMap<String, Integer>();
System.out.println("treemap输出");
mapadd(map2);
}
private static void mapadd(Map<String, Integer> map) {
map.put("a",1);
map.put("d",4);
map.put("c",3);
map.put("b",2);
for (Map.Entry<String,Integer> me:
map.entrySet()) {
System.out.println(me.getKey()+" "+me.getValue());
}
}
hashmap输出
a 1
b 2
c 3
d 4
treemap输出
a 1
b 2
c 3
d 4
二、定排序方式
(1)按照Key值进行排序
HashMap是根据Key的HashCode进行存储的,所以其Key值是没有顺序的不能针对其Key进行指定排序。
TreeMap可以根据Key值进行排序,有以下两种方式:
①new map的时候直接添加构造器comparator,此方法实现时默认对Key进行排序。
Map<String,Integer> map2 = new TreeMap<String, Integer>(new Comparator<String>() {
@Override
public int compare(String s, String t1) {
return t1.compareTo(s);//逆序排序
}
});
new
treemap输出
d 4
c 3
b 2
a 1
②new map时新建已将实现了comparator的类,默认对Key值进行排序。
Map<String,Integer> map2 = new TreeMap<String, Integer>(new MyComparator());
import java.util.Comparator;
public class MyComparator implements Comparator<String> {
@Override
public int compare(String o, String t1) {
return t1.compareTo(o);//逆序排序
}
}
treemap输出
d 4
c 3
b 2
a 1
(2)按照Value值进行排序
按照Value进行排序不能简单的利用上述方法了,要先将Map转换为list集合,根据集合的Collections.sort(List list, Comparator<? super T>
c)方法进行排序。HashMap和TreeMap实现方式相同,下面直接上代码:
import java.util.*;
class MapText{
public static void main(String[] args){
Map<String,Integer> map = new HashMap<String,Integer>();
System.out.println("hashmap输出");
mapadd(map);
List<Map.Entry<String,Integer>> maplist = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
//此时要注意只是对maplist改变了顺序,对map并没有改变顺序
Collections.sort(maplist, new Comparator<Map.Entry<String,Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> stringIntegerEntry, Map.Entry<String, Integer> t1) {
return t1.getValue()-stringIntegerEntry.getValue();//按照value值逆序排序
}
});
System.out.println("hash对value逆序排序输出");
for (Map.Entry<String,Integer> me:maplist) {
System.out.println(me.getKey()+":"+me.getValue());
}
System.out.println("对value排序后,map并未改变");
mapprint(map);//对map输出验证,其排列顺序并未发生改变
Map<String,Integer> map2 = new TreeMap<String, Integer>();
System.out.println("treemap输出");
mapadd(map2);
List<Map.Entry<String,Integer>> maplist2 = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
//此时要注意只是对maplist改变了顺序,对map并没有改变顺序
Collections.sort(maplist2, new Comparator<Map.Entry<String,Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> stringIntegerEntry, Map.Entry<String, Integer> t1) {
return t1.getValue()-stringIntegerEntry.getValue();//按照value值逆序排序
}
});
System.out.println("tree对value逆序排序输出");
for (Map.Entry<String,Integer> me:maplist) {
System.out.println(me.getKey()+":"+me.getValue());
}
System.out.println("对value排序后,map并未改变");
mapprint(map2);
}
private static void mapadd(Map<String, Integer> map) {
map.put("a",1);
map.put("d",4);
map.put("c",3);
map.put("b",2);
}
private static void mapprint(Map<String, Integer> map) {
for (Map.Entry<String,Integer> me:
map.entrySet()) {
System.out.println(me.getKey()+" "+me.getValue());
}
}
}
hashmap输出
hash对value逆序排序输出
d:4
c:3
b:2
a:1
对value排序后,map并未改变
a 1
b 2
c 3
d 4
treemap输出
tree对value逆序排序输出
d:4
c:3
b:2
a:1
对value排序后,map并未改变
a 1
b 2
c 3
d 4