Lambda 将映射中的流数据写回列表

Lambda 将映射中的流数据写回列表,lambda,java-8,java-stream,Lambda,Java 8,Java Stream,我有一个有信息的树形图。我可以浏览它并选择我想要打印的字段,或者只是从中创建一个类。但我无法从中找出转换并将其放入列表中 理想的情况下,我想把它作为 List<AssetMinuteTotals> listATM = assetsList.stream() .. 我试过这个。但这不会编译 assetsList.stream().collect(Collectors.groupingBy( Function.identity(), ()->new TreeMap<>

我有一个有信息的树形图。我可以浏览它并选择我想要打印的字段,或者只是从中创建一个类。但我无法从中找出转换并将其放入列表中

理想的情况下,我想把它作为

List<AssetMinuteTotals> listATM = assetsList.stream() ..
我试过这个。但这不会编译

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);
assetsList.stream().collect(Collectors.groupingBy(
Function.identity(),()->新树映射(组),
Collectors.SummaringDouble(资产::getPrice)))
.forEach((a,p)->新资产分钟总数(a.getPaper(),p.getAverage(),
(long)a.getTradeMinutesSinceMidnight(),p.getCount())->listAMT.add(e);
我可以通过将列表作为参数传递来解决这个问题,但这看起来非常难看,因为我必须在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));
assetsList.stream().collect(Collectors.groupingBy(
Function.identity(),()->新树映射(组),
Collectors.SummaringDouble(资产::getPrice)))
.forEach((a,p)->新资产分钟总数(a.getPaper(),p.getAverage(),
(long)a.getTradeMinutesSinceMidnight(),p.getCount(),listAMT));

您可能希望从
地图创建一个新的流,并收集它:

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());
List result=assetsList.stream()
.collect(收集器.groupingBy(Function.identity()),
()->新树形图(组),
Collectors.SummaringDouble(资产::getPrice)))
.entrySet().stream()
.map(条目->新资产分钟总数(条目.getKey().getPaper(),
entry.getValue().getAverage(),
(长)entry.getKey().getTradeMinutesSinceMidnight(),
entry.getValue().getCount())
.collect(Collectors.toList());
虽然我会将其分配给中间变量,因为这看起来更清晰:

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());
TreeMap map=assetsList.stream()
.collect(收集器.groupingBy(Function.identity()),
()->新树形图(组),
Collectors.summaringdouble(资产::getPrice));
列表结果=映射
.entrySet().stream()
.map(条目->新资产分钟总数(条目.getKey().getPaper(),
entry.getValue().getAverage(),
(长)entry.getKey().getTradeMinutesSinceMidnight(),
entry.getValue().getCount())
.collect(Collectors.toList());

如果能提供一个关于如何实现它的建议,那就太好了。如果你先做了彻底的尝试,那就太好了。
e
?什么是
e
?为什么你会对没有平衡括号的代码片段不能编译感到惊讶?如果你想为
循环而不是lambda表达式编写
,你真的会这样做吗考虑将加法移动到对象的构造函数中吗?这两个都是很棒的解决方案。我只是不知道如何从一个流继续到下一个流。
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());
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());