
在日常的软件开发中,我们经常会遇到需要处理包含重复数据的列表。例如,一个学生列表中可能存在多个具有相同学号(id)但不同入学日期(startdatetime)的记录,而我们只希望保留每个学号对应的最新记录。传统的方法可能涉及循环遍历、使用辅助set或map进行手动去重和比较,这往往导致代码冗长且可读性差。java 8引入的stream api提供了一种更简洁、更具函数式编程风格的解决方案。
我们的目标是:给定一个Student对象列表,如果存在多个Student对象拥有相同的id,则只保留其中startDatetime最新的那个对象。
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.Comparator;
import java.util.stream.Collectors;
public class StudentDeduplication {
static class Student {
private String id;
private LocalDateTime startDatetime;
public Student(String id, LocalDateTime startDatetime) {
this.id = id;
this.startDatetime = startDatetime;
}
public String getId() {
return id;
}
public LocalDateTime getStartDatetime() {
return startDatetime;
}
@Override
public String toString() {
return "Student{id='" + id + "', startDatetime=" + startDatetime + '}';
}
}
public static void main(String[] args) {
List<Student> students = new ArrayList<>() {{
add(new Student("1", LocalDateTime.now())); // 最新的ID为1的记录
add(new Student("1", LocalDateTime.of(2000, 2, 1, 1, 1)));
add(new Student("1", LocalDateTime.of(1990, 2, 1, 1, 1)));
add(new Student("2", LocalDateTime.of(1990, 2, 1, 1, 1))); // ID为2的记录
}};
System.out.println("原始学生列表:");
students.forEach(System.out::println);
// 使用Stream API进行去重和筛选
List<Student> uniqueStudents = students.stream()
.collect(Collectors.toMap(
Student::getId, // Key Mapper: 使用Student的ID作为Map的键
Function.identity(), // Value Mapper: 将Student对象本身作为Map的值
BinaryOperator.maxBy(Comparator.comparing(Student::getStartDatetime)) // Merge Function: 处理键冲突时,保留startDatetime最新的Student对象
))
.values().stream() // 获取Map中所有值(即去重后的Student对象)
.sorted(Comparator.comparing(Student::getStartDatetime)) // (可选) 按照startDatetime排序结果
.collect(Collectors.toList()); // 收集为List (Java 16+ 可用 .toList())
System.out.println("\n去重并筛选后的学生列表:");
uniqueStudents.forEach(System.out::println);
}
}上述解决方案的核心在于Collectors.toMap的第三个参数——合并函数(mergeFunction)。
keyMapper (Student::getId): 这个函数定义了如何从流中的每个元素(Student对象)中提取用于Map键的值。在这里,我们使用Student::getId,表示每个Student对象的id属性将作为Map的键。
valueMapper (Function.identity()): 这个函数定义了如何从流中的每个元素中提取用于Map值的值。Function.identity()是一个便利的方法,它返回一个函数,该函数简单地返回其输入参数。这意味着Student对象本身将作为Map的值。
mergeFunction (BinaryOperator.maxBy(Comparator.comparing(Student::getStartDatetime))): 这是解决重复问题的关键。当Collectors.toMap在处理流时遇到两个或多个元素生成了相同的键时,mergeFunction就会被调用来决定保留哪个值。
综合起来,当遇到相同id的Student对象时,BinaryOperator.maxBy(Comparator.comparing(Student::getStartDatetime))会比较这两个Student的startDatetime,并保留日期时间更晚(即最新)的那个对象。
Collectors.toMap操作的结果是一个Map<String, Student>,其中键是Student的ID,值是对应ID下最新的Student对象。要将其转换回List<Student>,我们只需:
立即学习“Java免费学习笔记(深入)”;
通过上述方法,我们可以利用Java Stream API的强大功能,以一种声明式且高效的方式,解决列表中对象的去重和筛选问题,使代码更加简洁、健壮。
以上就是使用Java Stream API高效处理列表去重:基于ID和日期筛选最新记录的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号