Java17之使用Lambda表达式对对象集合中指定的字段进行排序
文章目录
- Java17之使用Lambda表达式对对象集合中指定的字段进行排序
- 1. 集合对象排序
- 1. Java实体类
- 2. 正序排序
- 3.倒序排序
1. 集合对象排序
Java8起可用 List 的
sort
方法进行排序,形参为函数式接口Comparator
;可通过
Comparator.comparing
指定排序字段,如果需要指定多个字段排序,可使用thenComparing
来指定。排序规则:
- 默认使用正序规则排序,
Comparator.naturalOrder()
;- 如果想按倒序规则排序可以使用
Comparator.reverseOrder()
来指定。
- 以下案例使用Mybaits-plus查询数据,并对对象集合中指定的多个字段进行排序。
1. Java实体类
标注了Mybaits-plus及Lombok注解的实体类
CheckEntrustProject.java
如下
package com.yuan.entrust.entity;import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.EqualsAndHashCode;import java.io.Serial;@Data
@EqualsAndHashCode(callSuper = true)
@TableName("check_entrust_project")
public class CheckEntrustProject {@Serialprivate static final long serialVersionUID = 1L;/*** 主键*/@TableId(value = "id")private Long id;/*** 项目id*/private Long projectId;/*** 委托id*/private Long entrustId;/*** 项目名称*/private String projectName;/*** 天数*/private Integer days;/*** 频次*/private Integer frequency;/*** total = 天数 * 频次*/@TableField(exist = false)private Integer total;/*** 点位名称*/@TableField(exist = false)private String pointName;/*** 检测类别(项目分类)*/@TableField(exist = false)private String checkCategory;/*** 检查类型(项目类型)*/@TableField(exist = false)private String checkType;
}
2. 正序排序
@Autowired
private final CheckEntrustPointMapper checkEntrustPointMapper;
@GetMapping("/taskReceiveList/{entrustId}")public List<CheckEntrustProject> taskReceiveList(@PathVariable("entrustId") long entrustId) {QueryWrapper<CheckEntrustProject> projectQueryWrapper = new QueryWrapper<>();projectQueryWrapper.eq("entrust_id", entrustId);projectQueryWrapper.eq("del_flag", 0);projectQueryWrapper.orderByAsc("project_name");List<CheckEntrustProject> checkEntrustProjectList = checkEntrustProjectMapper.selectList(projectQueryWrapper);//默认使用Comparator.naturalOrder()排序,根据pointName、checkCategory、checkType等正序排序checkEntrustProjectList.sort(Comparator.comparing(CheckEntrustProject::getPointName).thenComparing(CheckEntrustProject::getCheckCategory).thenComparing(CheckEntrustProject::getCheckType));return checkEntrustProjectList;}
正序排序结果
3.倒序排序
@Autowired
private final CheckEntrustPointMapper checkEntrustPointMapper;
@GetMapping("/taskReceiveList/{entrustId}") public List<CheckEntrustProject> taskReceiveListxx(@PathVariable("entrustId") long entrustId) {QueryWrapper<CheckEntrustProject> projectQueryWrapper = new QueryWrapper<>();projectQueryWrapper.eq("entrust_id", entrustId);projectQueryWrapper.eq("del_flag", 0);projectQueryWrapper.orderByAsc("project_name");List<CheckEntrustProject> checkEntrustProjectList = checkEntrustProjectMapper.selectList(projectQueryWrapper);//使用Comparator.reverseOrder()指定倒序排序规则,根据pointName、checkCategory、checkType等倒序排序checkEntrustProjectList.sort(Comparator.comparing(CheckEntrustProject::getPointName,Comparator.reverseOrder()).thenComparing(CheckEntrustProject::getCheckCategory,Comparator.reverseOrder()).thenComparing(CheckEntrustProject::getCheckType,Comparator.reverseOrder()));return checkEntrustProjectList;}
倒序排序结果如下