当前位置: 首页 > article >正文

lambda表达式thenComparing使用示例

需求:要实现对 List 导出结果按工号从小到大排序,每个工号的足迹内容按照时间顺序倒序,同一个时间不同生效序号倒序展示

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

// JobFootprintVO 类定义
@Data
public class JobFootprintVO implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "员工工号")
    private String emplid;

    @ApiModelProperty(value = "时间")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private LocalDate effdt;

    @ApiModelProperty(value = "序号")
    private Integer effseq;
}

public class Main {
    public static void main(String[] args) {
        // 模拟数据
        List<JobFootprintVO> jobFootprintVOs = new ArrayList<>();
        JobFootprintVO vo1 = new JobFootprintVO();
        vo1.setEmplid("002");
        vo1.setEffdt(LocalDate.of(2025, 2, 18));
        vo1.setEffseq(1);
        jobFootprintVOs.add(vo1);

        JobFootprintVO vo2 = new JobFootprintVO();
        vo2.setEmplid("001");
        vo2.setEffdt(LocalDate.of(2025, 2, 17));
        vo2.setEffseq(2);
        jobFootprintVOs.add(vo2);

        JobFootprintVO vo3 = new JobFootprintVO();
        vo3.setEmplid("001");
        vo3.setEffdt(LocalDate.of(2025, 2, 17));
        vo3.setEffseq(1);
        jobFootprintVOs.add(vo3);

        // 排序操作
        List<JobFootprintVO> sortedList = sortJobFootprintVOs(jobFootprintVOs);

        // 输出排序后的结果
        for (JobFootprintVO vo : sortedList) {
            System.out.println("工号: " + vo.getEmplid() + ", 时间: " + vo.getEffdt() + ", 序号: " + vo.getEffseq());
        }
    }

    public static List<JobFootprintVO> sortJobFootprintVOs(List<JobFootprintVO> jobFootprintVOs) {
        return jobFootprintVOs.stream()
               .sorted(Comparator.comparing(JobFootprintVO::getEmplid)
                       .thenComparing(JobFootprintVO::getEffdt, Comparator.reverseOrder())
                       .thenComparing(JobFootprintVO::getEffseq, Comparator.reverseOrder()))
               .toList();
    }
}

http://www.kler.cn/a/552732.html

相关文章:

  • Ubuntu 下创建具有 root 权限用户
  • Elasticsearch AI Assistant 集成 DeepSeek,1分钟搭建智能运维助手
  • 在nodejs中使用RabbitMQ(七)实现生产者确认
  • 私域流量运营中用户价值提升策略研究——以开源AI智能名片2+1链动模式与S2B2C商城小程序为例
  • 1-13 tortoiseGit忽略文件与文件夹
  • 深度学习模型常用激活函数集合
  • 智能硬件定位技术发展趋势
  • HarmonyOS:使用List实现分组列表(包含粘性标题)
  • 中电金信:数字基础设施未来展望·行业定制与开源融合
  • JSON类型理解(前后端交互/内存对数据操作)
  • 微服务监控与Go服务性能分析
  • 级联选择器多选动态加载
  • 基于SpringBoot+Vue高校就业领航管理系统
  • ollama离线环境部署deepseek及对话网站开发
  • mybatis 结合oracle存储过程返回list
  • GPT-Sovits:语音克隆训练-遇坑解决
  • PostgreSQL认证指南
  • 力扣LeetCode: 931 下降路径最小和
  • 利用docker-compose一键创建并启动所有容器
  • Leetcode刷题面试2025