的高效方法 "> 的高效方法 " />
本文将深入探讨如何使用 Java 8 Stream API 将一个 List核心思路
问题的关键在于如何处理嵌套的集合结构。我们需要将 Trip 对象中的 Employee 列表扁平化,以便能够基于 empId 进行分组。这可以通过引入一个辅助对象来实现,该对象同时持有 empId 和 Trip 实例的引用。
辅助对象:TripEmployee (Java 16+ record)
从 Java 16 开始,可以使用 record 关键字来简洁地定义一个不可变的数据类,非常适合作为辅助对象。
public record TripEmployee(String empId, Trip trip) {}如果使用 Java 8,则需要创建一个普通的 class。
Java 8 实现 (class)
public class TripEmployee {
private String empId;
private Trip trip;
public TripEmployee(String empId, Trip trip) {
this.empId = empId;
this.trip = trip;
}
public String getEmpId() {
return empId;
}
public Trip getTrip() {
return trip;
}
}代码实现
以下是使用 Stream API 实现转换的代码示例:
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
public class TripMappingExample {
@Data
@NoArgsConstructor
@AllArgsConstructor
static class Trip {
private Date startTime;
private Date endTime;
List empList;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
static class Employee {
private String name;
private String empId;
}
public record TripEmployee(String empId, Trip trip) {}
public static void main(String[] args) {
// 示例数据
List trips = new ArrayList<>();
// 创建一些 Employee 对象
Employee emp1 = new Employee("Alice", "E123");
Employee emp2 = new Employee("Bob", "E456");
Employee emp3 = new Employee("Charlie"
, "E123"); // 故意重复一个 empId
// 创建包含 Employee 列表的 Trip 对象
Trip trip1 = new Trip(new Date(), new Date(), List.of(emp1, emp2));
Trip trip2 = new Trip(new Date(), new Date(), List.of(emp2, emp3));
trips.add(trip1);
trips.add(trip2);
// 使用 Stream API 进行转换
Map> empMap = trips.stream()
.flatMap(trip -> trip.getEmpList().stream()
.map(emp -> new TripEmployee(emp.getEmpId(), trip)))
.collect(Collectors.groupingBy(
TripEmployee::empId,
Collectors.mapping(TripEmployee::trip,
Collectors.toList())));
// 打印结果
empMap.forEach((empId, tripList) -> {
System.out.println("Employee ID: " + empId);
tripList.forEach(trip -> System.out.println(" Trip: " + trip));
});
}
} 代码解释
注意事项
总结
本文提供了一种使用 Java 8 Stream API 将 List