将流中的流数据写回List

时间:2021-08-23 19:04:30

I have a treemap with information. I can foreach through it and pick the fields I want to print or just create a class from. But I can't figure out the conversion from that and get it into a List<>

我有一个包含信息的树形图。我可以通过它来选择我想要打印的字段,或者只是创建一个类。但我无法弄清楚转换并将其转换为List <>

Ideally I would like to have it returned as

理想情况下,我希望将其作为返回

List<AssetMinuteTotals> listATM = assetsList.stream() ..

Or in another form that makes the listATM contain the AssetMinuteTotals list. I can't use collect since that is not part of TreeMap. As you can see below I have tried list.add. But that syntax is apparently not correct.

或者以另一种形式使listATM包含AssetMinuteTotals列表。我不能使用collect,因为它不是TreeMap的一部分。如下所示,我尝试过list.add。但是这种语法显然不正确。

assetsList.stream().collect(Collectors.groupingBy(
Function.identity(), ()->new TreeMap<>(group),
Collectors.summarizingDouble(Asset::getPrice)))
.forEach((a,p)-> new AssetMinuteTotals(a.getPaper(), p.getAverage(), 
                    (long)a.getTradeMinutesSinceMidnight(), p.getCount()));

The group is defined this way:

该组的定义方式如下:

Comparator<Asset> group=Comparator.comparing(Asset::getPaper)
    .thenComparing(Asset::getTradeMinutesSinceMidnight);

I have tried with this. But that will not compile.

我试过这个。但那不会编译。

assetsList.stream().collect(Collectors.groupingBy(
Function.identity(), ()->new TreeMap<>(group),
Collectors.summarizingDouble(Asset::getPrice)))
.forEach((a,p)-> new AssetMinuteTotals(a.getPaper(), p.getAverage(), 
                    (long)a.getTradeMinutesSinceMidnight(), p.getCount()) -> listAMT.add(e);

I can solve this by passing the list as an argument. But that seems very ugly since I have to implement that in the constructor of the AssetMinuteTotals class

我可以通过将列表作为参数传递来解决这个问题。但这似乎非常难看,因为我必须在AssetMinuteTotals类的构造函数中实现它

assetsList.stream().collect(Collectors.groupingBy(
Function.identity(), ()->new TreeMap<>(group),
Collectors.summarizingDouble(Asset::getPrice)))
.forEach((a,p)-> new AssetMinuteTotals(a.getPaper(), p.getAverage(), 
                    (long)a.getTradeMinutesSinceMidnight(), p.getCount(), listAMT));

1 个解决方案

#1


It's likely that you want to create a new stream from the Map and collect it:

您可能希望从Map创建新流并收集它:

List<AssetMinuteTotals> result = assetsList.stream()
        .collect(Collectors.groupingBy(Function.identity(),
                        () -> new TreeMap<>(group),
                        Collectors.summarizingDouble(Asset::getPrice)))
        .entrySet().stream()
        .map(entry -> new AssetMinuteTotals(entry.getKey().getPaper(),
                entry.getValue().getAverage(), 
                (long) entry.getKey().getTradeMinutesSinceMidnight(),
                entry.getValue().getCount()))
        .collect(Collectors.toList());

Though I would assign it to the intermediate variable as this looks more clean for me:

虽然我会将它分配给中间变量,因为这对我来说看起来更干净:

TreeMap<Asset, DoubleSummaryStatistics> map = assetsList.stream()
        .collect(Collectors.groupingBy(Function.identity(),
                        () -> new TreeMap<>(group),
                        Collectors.summarizingDouble(Asset::getPrice)));
List<AssetMinuteTotals> result = map
        .entrySet().stream()
        .map(entry -> new AssetMinuteTotals(entry.getKey().getPaper(),
                entry.getValue().getAverage(), 
                (long) entry.getKey().getTradeMinutesSinceMidnight(),
                entry.getValue().getCount()))
        .collect(Collectors.toList());

#1


It's likely that you want to create a new stream from the Map and collect it:

您可能希望从Map创建新流并收集它:

List<AssetMinuteTotals> result = assetsList.stream()
        .collect(Collectors.groupingBy(Function.identity(),
                        () -> new TreeMap<>(group),
                        Collectors.summarizingDouble(Asset::getPrice)))
        .entrySet().stream()
        .map(entry -> new AssetMinuteTotals(entry.getKey().getPaper(),
                entry.getValue().getAverage(), 
                (long) entry.getKey().getTradeMinutesSinceMidnight(),
                entry.getValue().getCount()))
        .collect(Collectors.toList());

Though I would assign it to the intermediate variable as this looks more clean for me:

虽然我会将它分配给中间变量,因为这对我来说看起来更干净:

TreeMap<Asset, DoubleSummaryStatistics> map = assetsList.stream()
        .collect(Collectors.groupingBy(Function.identity(),
                        () -> new TreeMap<>(group),
                        Collectors.summarizingDouble(Asset::getPrice)));
List<AssetMinuteTotals> result = map
        .entrySet().stream()
        .map(entry -> new AssetMinuteTotals(entry.getKey().getPaper(),
                entry.getValue().getAverage(), 
                (long) entry.getKey().getTradeMinutesSinceMidnight(),
                entry.getValue().getCount()))
        .collect(Collectors.toList());