Java对List<Map>中的Map的某个值进行排序
在多表查询的过程中常常会遇到查询多个字段组成的集合,并且还要对结果集合里面的某个属性进行排序返回。
1、使用Collections.sort()
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author liuxingyu01
* @version 创建时间:2021-4-29 11:11:25
* @description
*/
public class ListMapSort {
public static void main(String[] args) {
List<Map<String, Object>> alarmMaps = new ArrayList();
Map<String, Object> newMap = new HashMap<>();
newMap.put("age", 2);
Map<String, Object> newMap1 = new HashMap<>();
newMap1.put("age", 7);
Map<String, Object> newMap2 = new HashMap<>();
newMap2.put("age", 1);
Map<String, Object> newMap3 = new HashMap<>();
newMap3.put("age", 9);
Map<String, Object> newMap4 = new HashMap<>();
newMap4.put("age", 23);
alarmMaps.add(newMap);
alarmMaps.add(newMap1);
alarmMaps.add(newMap2);
alarmMaps.add(newMap3);
alarmMaps.add(newMap4);
// 排序 - 匿名方式
if (alarmMaps != null && alarmMaps.size() > 1) {
Collections.sort(alarmMaps, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
Integer o1Value = Integer.valueOf(o1.get("age").toString());
Integer o2Value = Integer.valueOf(o2.get("age").toString());
// return o2Value.compareTo(o1Value); // 倒序
return o1Value.compareTo(o2Value); // 正序
}
});
}
System.out.println(alarmMaps.toString());
// lambda表达式方式(只有jdk8才能用)
if (alarmMaps != null && alarmMaps.size() > 1) {
Collections.sort(alarmMaps, (o1, o2) -> {
Integer o1Value = Integer.valueOf(o1.get("age").toString());
Integer o2Value = Integer.valueOf(o2.get("age").toString());
return o1Value.compareTo(o2Value);
});
}
System.out.println(alarmMaps.toString());
}
} 执行结果:
2、使用list.stream().sorted -->只有jdk8可用
(1)对于Map
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author liuxingyu01
* @version 创建时间:2021-4-29 11:11:25
* @description
*/
public class ListMapSort {
public static void main(String[] args) {
List<Map<String, Object>> alarmMaps = new ArrayList();
Map<String, Object> newMap = new HashMap<>();
newMap.put("age", 2);
Map<String, Object> newMap1 = new HashMap<>();
newMap1.put("age", 7);
Map<String, Object> newMap2 = new HashMap<>();
newMap2.put("age", 1);
Map<String, Object> newMap3 = new HashMap<>();
newMap3.put("age", 9);
Map<String, Object> newMap4 = new HashMap<>();
newMap4.put("age", 23);
alarmMaps.add(newMap);
alarmMaps.add(newMap1);
alarmMaps.add(newMap2);
alarmMaps.add(newMap3);
alarmMaps.add(newMap4);
// 使用list.stream().sorted 正序
List<Map<String, Object>> newList = alarmMaps.stream().sorted(Comparator.comparing(ListMapSort::comparingByAge))
.collect(Collectors.toList());
System.out.println(newList.toString());
// 使用list.stream().sorted 倒序
List<Map<String, Object>> newList2 = alarmMaps.stream().sorted(Comparator.comparing(ListMapSort::comparingByAge).reversed())
.collect(Collectors.toList());
System.out.println(newList2.toString());
}
private static Integer comparingByAge(Map<String, Object> map) {
return Integer.valueOf(map.get("age").toString());
}
} 执行结果:
(2)对于javabean
新建StudentInfo类
package java8;
import java.time.LocalDate;
import java.util.List;
/**
* @author liuxingyu01
* @version 创建时间:2021-4-29 14:54:21
* @description
*/
public class StudentInfo implements Comparable<StudentInfo>{
//名称
private String name;
//性别 true男 false女
private Boolean gender;
//年龄
private Integer age;
//身高
private Double height;
//出生日期
private LocalDate birthday;
public StudentInfo(String name, Boolean gender, Integer age, Double height, LocalDate birthday){
this.name = name;
this.gender = gender;
this.age = age;
this.height = height;
this.birthday = birthday;
}
public String toString(){
String info = String.format("%s\t\t%s\t\t%s\t\t\t%s\t\t%s",this.name,this.gender.toString(),this.age.toString(),this.height.toString(),birthday.toString());
return info;
}
public static void printStudents(List<StudentInfo> studentInfos){
System.out.println("[姓名]\t\t[性别]\t\t[年龄]\t\t[身高]\t\t[生日]");
System.out.println("----------------------------------------------------------");
studentInfos.forEach(s->System.out.println(s.toString()));
System.out.println(" ");
}
@Override
public int compareTo(StudentInfo ob) {
return this.age.compareTo(ob.getAge());
//return 1;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getGender() {
return gender;
}
public void setGender(Boolean gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getHeight() {
return height;
}
public void setHeight(Double height) {
this.height = height;
}
public LocalDate getBirthday() {
return birthday;
}
public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}
}
测试:
import java.util.stream.Collectors;
import java.time.LocalDate;
import java.util.List;
/**
* @author liuxingyu01
* @version 创建时间:2021-4-29 11:11:25
* @description
*/
public class ListMapSort {
public static void main(String[] args) {
// 测试数据,请不要纠结数据的严谨性
List<StudentInfo> studentList = new ArrayList<>();
studentList.add(new StudentInfo("李小明", true, 18, 1.76, LocalDate.of(2001, 3, 23)));
studentList.add(new StudentInfo("张小丽", false, 18, 1.61, LocalDate.of(2001, 6, 3)));
studentList.add(new StudentInfo("王大朋", true, 19, 1.82, LocalDate.of(2000, 3, 11)));
studentList.add(new StudentInfo("陈小跑", false, 17, 1.67, LocalDate.of(2002, 10, 18)));
/**********************************************使用年龄进行排序***************************************************************/
//排序前输出
StudentInfo.printStudents(studentList);
//按年龄排序(Integer类型)
List<StudentInfo> studentsSortName = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge)).collect(Collectors.toList());
//排序后输出
StudentInfo.printStudents(studentsSortName);
/**********************************************使用年龄进行降序排序(使用reversed()方法)***************************************************************/
//排序前输出
StudentInfo.printStudents(studentList);
//按年龄排序(Integer类型) --- 降序
List<StudentInfo> studentsSortNameRe = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge).reversed()).collect(Collectors.toList());
//排序后输出
StudentInfo.printStudents(studentsSortNameRe);
/**********************************************使用年龄进行降序排序,年龄相同再使用身高升序排序***************************************************************/
// 排序前输出
StudentInfo.printStudents(studentList);
// 按年龄排序(Integer类型) --- 降序
List<StudentInfo> studentsSortName3 = studentList.stream()
.sorted(Comparator.comparing(StudentInfo::getAge).reversed().thenComparing(StudentInfo::getHeight))
.collect(Collectors.toList());
// 排序后输出
StudentInfo.printStudents(studentsSortName3);
}
} 执行结果:

