使用 java 8 steam 查找两个列表之间的匹配行并映射到另一个对象

我试图在两个不同的列表之间找到匹配的元素,然后我试图映射到不同对象的列表,该列表包含来自列表和某些选定元素的元素。


这是我的两个豆子——


public class LogsData {

    LocalDate eventDate ;

    String correlationId;

    String authId;

    int numberofSQL;

    // getter and setter

}

public class DistributionData {

    LocalDate eventDate;

    String correlationId;

    String callingProId;

    long transactionCount ;

    // getter and setter 

}

public class ResultBean {

    LocalDate eventDate;

    String correlationId;

    String callingProId;

    long transactionCount ;

    String authId ;

    int numberOfSQL;

}

我如何获得包含与 匹配的最终列表correlationId,eventDate在结果中,我想要ResultBean.


有人可以帮忙吗?


慕勒3428872
浏览 381回答 3
3回答

浮云间

我改变了一点点设计以使其工作,我将分发列表添加到 Logs Beans 中,因为它具有一对多的关系,我能够合并来自两个列表的数据,但它运行速度非常慢,因为集合大小非常大,尤其是分发列表,有人可以建议一些高性能或任何更好的设计。&nbsp;List<LogsData> logsData = logs.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(e -> (distributionData.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(d ->&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;d.getCorrelationId.equals(e.getCorrelationId))&nbsp; &nbsp; &nbsp; &nbsp;.filter(d-> d.getEventDate().equals(e.getEventDate()))&nbsp; &nbsp; &nbsp; &nbsp;.map(mapper-> e.getDistribution().add(mapper); return e; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .count())<1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());&nbsp;

智慧大石

根据Louis Wasserman 的评论,可能可以在 Streams 有优势的地方使用它。考虑拥有LogsData和DistributionData(和可选的ResultBean)扩展基类型:class Data {&nbsp; &nbsp; LocalDate eventDate ;&nbsp; &nbsp; String correlationId;&nbsp; &nbsp; Data(LocalDate eventDate, String correlationId) {&nbsp; &nbsp; &nbsp; &nbsp; this.eventDate = eventDate;&nbsp; &nbsp; &nbsp; &nbsp; this.correlationId = correlationId;&nbsp; &nbsp; }&nbsp; &nbsp; LocalDate getEventDate() { return eventDate; }&nbsp; &nbsp; String getCorrelationId(){ return correlationId; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean equals(Object o) {&nbsp; &nbsp; &nbsp; &nbsp; if(!(o instanceof Data)) { return false; }&nbsp; &nbsp; &nbsp; &nbsp; Data d = (Data) o;&nbsp; &nbsp; &nbsp; &nbsp; return eventDate.equals(d.getEventDate())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && correlationId.equals(d.getCorrelationId() );&nbsp; &nbsp; }}列出:List<LogsData> logsData = new ArrayList<>();List<DistributionData> dData = new ArrayList<>();您可以简单地将两个列表相交logsData.retainAll(dData);dData.retainAll(logsData);让它们以相同的顺序排序://sort so two lists have the same order. If correlationId is not unique you may need&nbsp;//to enhance the comperator&nbsp;Collections.sort(dData, (a, b) -> a.getCorrelationId().compareToIgnoreCase(b.getCorrelationId()));Collections.sort(logsData, (a, b) -> a.getCorrelationId().compareToIgnoreCase(b.getCorrelationId()));并用于Stream构造ResultBean对象列表:List<ResultBean> resultList = IntStream.range(0, logsData.size())&nbsp; &nbsp; .mapToObj( i ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ResultBean(dData.get(i).getEventDate(), dData.get(i).getCorrelationId(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dData.get(i).getCallingProId(), dData.get(i).getTransactionCount(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; logsData.get(i).getAuthId(), logsData.get(i).getNumberofSQL())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; .collect(Collectors.toList());

UYOU

一种方法是将logsData列表的元素累积到一个映射中,其中键logsData#getEventDate与logsData#getCorrelationId.Map<String, LogsData> accumulator =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; logsData.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(toMap(l -> l.getEventDate() + l.getCorrelationId(), Function.identity()));然后流过distributionData列表并获取地图中的相应元素,然后将它们转换为ResultBean.List<ResultBean> resultSet = distributionData.stream()&nbsp; &nbsp; &nbsp; &nbsp;.map(d -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LogsData logs = accumulator.get(d.getEventDate() + d.getCorrelationId());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (logs != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return new ResultBean(d.getEventDate(), d.getCorrelationId(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;d.getCallingProId(), d.getTransactionCount(), logs.getAuthId(), logs.getNumberofSQL());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return null;&nbsp; &nbsp; &nbsp; &nbsp;})&nbsp; &nbsp; &nbsp; &nbsp;.filter(Objects::nonNull)&nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.toList());这假设ResultBean有一个构造函数接受所有必要的参数,如果不是这样,那么只需调用 setter 方法来设置必要的数据。进口:import static java.util.stream.Collectors.*;import java.util.stream.*;import java.util.function.*;另一种方法虽然效率较低:List<ResultBean> resultSet = logsData.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(l -> distributionData.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(d -> l.getEventDate().equals(d.getEventDate()) &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l.getCorrelationId().equals(d.getCorrelationId()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .findFirst()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(d -> new ResultBean(d.getEventDate(), d.getCorrelationId(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d.getCallingProId(), d.getTransactionCount(), l.getAuthId(), l.getNumberofSQL()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .orElse(null))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(Objects::nonNull)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());略有不同,在 JDK9 中避免了这种.orElse(null)).filter(Objects::nonNull)模式:List<ResultBean> resultSet = logsData.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .flatMap(l -> distributionData.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(d -> l.getEventDate().equals(d.getEventDate()) &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l.getCorrelationId().equals(d.getCorrelationId()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .findFirst()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(d -> new ResultBean(d.getEventDate(), d.getCorrelationId(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d.getCallingProId(), d.getTransactionCount(), l.getAuthId(), l.getNumberofSQL()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java