3 回答
TA贡献1829条经验 获得超4个赞
我改变了一点点设计以使其工作,我将分发列表添加到 Logs Beans 中,因为它具有一对多的关系,我能够合并来自两个列表的数据,但它运行速度非常慢,因为集合大小非常大,尤其是分发列表,有人可以建议一些高性能或任何更好的设计。
List<LogsData> logsData = logs.stream()
.filter(e -> (distributionData.stream()
.filter(d ->
d.getCorrelationId.equals(e.getCorrelationId))
.filter(d-> d.getEventDate().equals(e.getEventDate()))
.map(mapper-> e.getDistribution().add(mapper); return e; })
.count())<1)
.collect(Collectors.toList());
TA贡献1946条经验 获得超3个赞
根据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());
TA贡献1878条经验 获得超4个赞
一种方法是将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());
添加回答
举报