智慧大石
根据Louis Wasserman 的评论,可能可以在 Streams 有优势的地方使用它。考虑拥有LogsData和DistributionData(和可选的ResultBean)扩展基类型:class Data { LocalDate eventDate ; String correlationId; Data(LocalDate eventDate, String correlationId) { this.eventDate = eventDate; this.correlationId = correlationId; } LocalDate getEventDate() { return eventDate; } String getCorrelationId(){ return correlationId; } @Override public boolean equals(Object o) { if(!(o instanceof Data)) { return false; } Data d = (Data) o; return eventDate.equals(d.getEventDate()) && correlationId.equals(d.getCorrelationId() ); }}列出: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 //to enhance the comperator 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()) .mapToObj( i -> new ResultBean(dData.get(i).getEventDate(), dData.get(i).getCorrelationId(), dData.get(i).getCallingProId(), dData.get(i).getTransactionCount(), logsData.get(i).getAuthId(), logsData.get(i).getNumberofSQL()) ) .collect(Collectors.toList());
UYOU
一种方法是将logsData列表的元素累积到一个映射中,其中键logsData#getEventDate与logsData#getCorrelationId.Map<String, LogsData> accumulator = logsData.stream() .collect(toMap(l -> l.getEventDate() + l.getCorrelationId(), Function.identity()));然后流过distributionData列表并获取地图中的相应元素,然后将它们转换为ResultBean.List<ResultBean> resultSet = distributionData.stream() .map(d -> { LogsData logs = accumulator.get(d.getEventDate() + d.getCorrelationId()); if (logs != null) return new ResultBean(d.getEventDate(), d.getCorrelationId(), d.getCallingProId(), d.getTransactionCount(), logs.getAuthId(), logs.getNumberofSQL()); return null; }) .filter(Objects::nonNull) .collect(Collectors.toList());这假设ResultBean有一个构造函数接受所有必要的参数,如果不是这样,那么只需调用 setter 方法来设置必要的数据。进口:import static java.util.stream.Collectors.*;import java.util.stream.*;import java.util.function.*;另一种方法虽然效率较低:List<ResultBean> resultSet = logsData.stream() .map(l -> distributionData.stream() .filter(d -> l.getEventDate().equals(d.getEventDate()) && l.getCorrelationId().equals(d.getCorrelationId())) .findFirst() .map(d -> new ResultBean(d.getEventDate(), d.getCorrelationId(), d.getCallingProId(), d.getTransactionCount(), l.getAuthId(), l.getNumberofSQL())) .orElse(null)) .filter(Objects::nonNull) .collect(Collectors.toList());略有不同,在 JDK9 中避免了这种.orElse(null)).filter(Objects::nonNull)模式:List<ResultBean> resultSet = logsData.stream() .flatMap(l -> distributionData.stream() .filter(d -> l.getEventDate().equals(d.getEventDate()) && l.getCorrelationId().equals(d.getCorrelationId())) .findFirst() .map(d -> new ResultBean(d.getEventDate(), d.getCorrelationId(), d.getCallingProId(), d.getTransactionCount(), l.getAuthId(), l.getNumberofSQL())) .stream() ) .collect(Collectors.toList());