Java语言利用Collections.sort对Map,List排序
1.main方法包含TreeMap排序1,TreeMap排序2,HashMap排序,List排序,List排序,List
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() {
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", 3);
treeMap2.put("b", 4);
treeMap2.put("a", 7);
treeMap2.put("c", 1);
// 转换成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", 3);
hashMap.put("a", 2);
hashMap.put("b", 1);
hashMap.put("d", 4);
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(3);
nums.add(5);
nums.add(2);
nums.add(1);
// 升序排序(默认)
Collections.sort(nums);
// 排序后输出
System.out.println(nums);
// List<Bean>排序
List<User> users = new ArrayList<User>();
users.add(new User(2, "jack"));
users.add(new User(1, "tom"));
users.add(new User(3, "keck"));
users.add(new User(4, "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", 20);
map.put("sex", 1);
listMap.add(map);
Map<String, Integer> map2 = new HashMap<>();
map2.put("age", 29);
map2.put("sex", 2);
listMap.add(map2);
Map<String, Integer> map3 = new HashMap<>();
map3.put("age", 35);
map3.put("sex", 1);
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.java类:
package com.tao.test;
public class User implements Comparable{
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升序排序
}
}
转载自https://www.cnblogs.com/stromgao/p/12185697.html?ivk_sa=1024320u
自行补充
rankList= super.queryForList(rankSql,rankMap);
//排序,打分排名
Collections.sort(rankList, new Comparator<Map<String, Integer>>() {
@Override
public int compare(Map<String, Integer> o1, Map<String, Integer> o2){
if(MapUtils.getInteger(o1,"INTEGRAL")!=null&&MapUtils.getInteger(o2,"INTEGRAL")!=null){
if(MapUtils.getInteger(o1,"INTEGRAL") < MapUtils.getInteger(o2,"INTEGRAL")){
return -1;
}else if (MapUtils.getInteger(o1,"INTEGRAL") >MapUtils.getInteger(o2,"INTEGRAL")){
return 1;
}else {
return 0;
}
}
return 0;
// return (MapUtils.getInteger(o1,"INTEGRAL") < MapUtils.getInteger(o2,"INTEGRAL")) ? -1 : ((MapUtils.getInteger(o1,"INTEGRAL") == MapUtils.getInteger(o2,"INTEGRAL")) ? 0 : 1);//
return o1.get("INTEGRAL").compareTo(o2.get("INTEGRAL"));
}
});