我有一个对象列表。该对象如下所示:
public class Slots {
String slotType;
Visits visit;
}
public class Visits {
private long visitCode;
private String agendaCode;
private String scheduledTime;
private String resourceType;
private String resourceDescription;
private String visitTypeCode;
...
}
我需要找到具有相同元素的元素,agendaCode
而visitTypeCode
对于scheduledTime
我的生活,我无法完成它。
我试过这个:
Set<String> agendas = slotsResponse.getContent().stream()
.map(Slots::getVisit)
.map(Visits::getAgendaCode)
.collect(Collectors.toUnmodifiableSet());
Set<String> visitTypeCode = slotsResponse.getContent().stream()
.map(Slots::getVisit)
.map(Visits::getVisitTypeCode)
.collect(Collectors.toUnmodifiableSet());
Set<String> scheduledTime = slotsResponse.getContent().stream()
.map(Slots::getVisit)
.map(Visits::getScheduledTime)
.collect(Collectors.toUnmodifiableSet());
List<Slots> collect = slotsResponse.getContent().stream()
.filter(c -> agendas.contains(c.getVisit().getAgendaCode()))
.filter(c -> visitTypeCode.contains(c.getVisit().getVisitTypeCode()))
.filter(c -> scheduledTime.contains(c.getVisit().getScheduledTime()))
.collect(Collectors.toList());
但它并没有像我想象的那样做。理想情况下,我会有一个列表列表,其中每个子列表都是共享相同的 Slots 对象的列表agendaCode
,visitTypeCode
并且scheduledTime
. 我在函数式编程中挣扎,所以任何帮助或指针都会很棒!
这是 Java 11,我也在使用 vavr。