(一)treeHap
特点:treeMap中的元素根据键的大小自然排序(默认是升序)
1、treeHap遍历测试
import java.io.IOException;
import java.util.*;
public class Main {
public static void main(String[] args)throws IOException {
Map<String,Integer>map=new TreeMap<>();
map.put("b",2);
map.put("c",1);
map.put("d",3);
map.put("a",1);
for(Map.Entry<String,Integer> entry:map.entrySet()){
System.out.println(entry.getKey()+" "+entry.getValue());
}
}
}
输出:
a 1
b 2
c 1
d 3
2、自定义排序方式测试
自定义排序,选择value的大小作为比较依据。
import java.io.IOException;
import java.util.*;
public class Main {
public static void main(String[] args)throws IOException {
Map<String,Integer>map=new TreeMap<>();
map.put("b",2);
map.put("c",1);
map.put("d",3);
map.put("a",1);
List<Map.Entry<String,Integer>>list=new ArrayList<>(map.entrySet());
//排序之前
System.out.println("排序之前:");
for (int i = 0; i < list.size(); i++) {
String key=list.get(i).getKey();
int value=list.get(i).getValue();
System.out.println(key+" "+value);
}
//排序
list.sort(new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o1.getValue()-o2.getValue();//o1减o2是升序,反之是降序
}
});
//排序之后
System.out.println("排序之后:");
for (int i = 0; i < list.size(); i++) {
String key=list.get(i).getKey();
int value=list.get(i).getValue();
System.out.println(key+" "+value);
}
}
}
输出:
排序之前:
b 2
c 1
a 1
d 3
排序之后:
a 1
c 1
b 2
d 3
测试结果,先根据value大小进行升序排序,value相等则根据key排序。
(二)LinkedHashMap
(1)LinkedHashMap遍历测试
import java.io.IOException;
import java.util.*;
public class Main {
public static void main(String[] args)throws IOException {
Map<String,Integer>map=new LinkedHashMap<>();
map.put("b",2);
map.put("c",1);
map.put("d",3);
map.put("a",1);
for(Map.Entry<String,Integer> entry:map.entrySet()){
System.out.println(entry.getKey()+" "+entry.getValue());
}
}
}
输出:
b 2
c 1
d 3
a 1
(2)自定义排序测试
自定义排序,选择value的大小作为比较依据。
import java.io.IOException;
import java.util.*;
public class Main {
public static void main(String[] args)throws IOException {
Map<String,Integer>map=new LinkedHashMap<>();
map.put("b",2);
map.put("c",1);
map.put("a",1);
map.put("d",3);
List<Map.Entry<String,Integer>>list=new ArrayList<>(map.entrySet());
//排序之前
System.out.println("排序之前:");
for (int i = 0; i < list.size(); i++) {
String key=list.get(i).getKey();
int value=list.get(i).getValue();
System.out.println(key+" "+value);
}
//排序
list.sort(new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o1.getValue()-o2.getValue();//o1减o2是升序,反之是降序
}
});
//排序之后
System.out.println("排序之后:");
for (int i = 0; i < list.size(); i++) {
String key=list.get(i).getKey();
int value=list.get(i).getValue();
System.out.println(key+" "+value);
}
}
}
输出:
排序之前:
b 2
c 1
a 1
d 3
排序之后:
c 1
a 1
b 2
d 3
测试结果是,先根据value大小进行升序排序,value相等则根据元素的添加先后顺序排序。
(三)小结
- TreeMap可以实现根据key的大小自动进行升序排序。对其按照value的大小实现自定义排序时,会先先根据value的大小进行排序,value相等则根据key的大小排序
- LinkedHashMap中的元素会自动按照添加的先后顺序进行排序。对其按照value的大小实现自定义排序时,会先先根据value的大小进行排序,value相等则根据元素的添加先后顺序排序
补充:HashMap中的元素是无序的(不按照添加顺序输出,也不按照key的自然排序输出)。