Java语言利用Collections.sort对Map,List排序

1.main方法包含TreeMap排序1,TreeMap排序2,HashMap排序,List<Integer>排序,List<Bean>排序,List<Map>排序

package com.tao.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap; public class Sort {
public static void main(String[] args) {
// TreeMap排序1
Map<String, String> treeMap = new TreeMap<String, String>(new Comparator<String>() {
public int compare(String o1, String o2) {
// 升序排序
return o1.compareTo(o2);
}
});
treeMap.put("c", "ccccc");
treeMap.put("a", "aaaaa");
treeMap.put("b", "bbbbb");
treeMap.put("d", "ddddd");
// 排序后输出
for (String key : treeMap.keySet()) {
System.out.println("Key=" + key + ", Value=" + treeMap.get(key));
} // TreeMap排序2
Map<String, Integer> treeMap2 = new TreeMap<String, Integer>();
treeMap2.put("d", );
treeMap2.put("b", );
treeMap2.put("a", );
treeMap2.put("c", );
// 转换成list
List<Map.Entry<String, Integer>> treeList = new ArrayList<Map.Entry<String, Integer>>(treeMap2.entrySet());
Collections.sort(treeList, new Comparator<Map.Entry<String, Integer>>() {
// 升序排序
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
// 排序后输出
for (Map.Entry<String, Integer> m : treeList) {
System.out.println("Key=" + m.getKey() + ", Value=" + m.getValue());
} // HashMap排序
Map<String, Integer> hashMap = new HashMap<String, Integer>();
hashMap.put("c", );
hashMap.put("a", );
hashMap.put("b", );
hashMap.put("d", );
List<Map.Entry<String, Integer>> hashList = new ArrayList<Map.Entry<String, Integer>>(hashMap.entrySet());
Collections.sort(hashList, new Comparator<Map.Entry<String, Integer>>() {
// 升序排序
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
// 排序后输出
for (Map.Entry<String, Integer> m : hashList) {
System.out.println("Key=" + m.getKey() + ", Value=" + m.getValue());
} // List<Integer>排序
List<Integer> nums = new ArrayList<Integer>();
nums.add();
nums.add();
nums.add();
nums.add();
// 升序排序(默认)
Collections.sort(nums);
// 排序后输出
System.out.println(nums); // List<Bean>排序
List<User> users = new ArrayList<User>();
users.add(new User(, "jack"));
users.add(new User(, "tom"));
users.add(new User(, "keck"));
users.add(new User(, "tao"));
// id升序排序
Collections.sort(users);
// 排序后输出
for (User user : users) {
System.out.println(user.getId() + "," + user.getName());
} // List<Map>排序
List<Map<String, Integer>> listMap = new ArrayList<Map<String, Integer>>();
Map<String, Integer> map = new HashMap<>();
map.put("age", );
map.put("sex", );
listMap.add(map);
Map<String, Integer> map2 = new HashMap<>();
map2.put("age", );
map2.put("sex", );
listMap.add(map2);
Map<String, Integer> map3 = new HashMap<>();
map3.put("age", );
map3.put("sex", );
listMap.add(map3);
// 按照map值排序
Collections.sort(listMap, new Comparator<Map<String, Integer>>() {
@Override
public int compare(Map<String, Integer> o1, Map<String, Integer> o2) {
return o1.get("age").compareTo(o2.get("age"));// age升序排序
}
});
// 排序后输出
for (Map<String, Integer> m : listMap) {
System.out.println(m);
}
}
}

2.List<User>排序的User.java类:

package com.tao.test;

public class User implements Comparable<User>{

    private int id;
private String name; public User(int id, String name) {
super();
this.id = id;
this.name = name;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} @Override
public int compareTo(User o) {
return this.id - o.getId();//id升序排序
} }

3.运行截图

Java语言利用Collections.sort对Map,List排序

上一篇:stl map高效遍历删除的方法 [转]


下一篇:数据结构和算法(Golang实现)(25)排序算法-快速排序