将映射中的流数据写回列表
Writing stream data in map back to a List
我有一张包含信息的树状图。我可以通过它进行 foreach 并选择我想要打印的字段,或者只是从中创建一个 class。但我无法从中找出转换并将其放入 List<>
理想情况下,我希望它返回为
List<AssetMinuteTotals> listATM = assetsList.stream() ..
或者以另一种形式使 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()));
group
是这样定义的:
Comparator<Asset> group=Comparator.comparing(Asset::getPaper)
.thenComparing(Asset::getTradeMinutesSinceMidnight);
这个我试过了。但这不会编译。
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);
我可以通过将列表作为参数传递来解决这个问题。但这看起来很难看,因为我必须在 AssetMinuteTotals class
的构造函数中实现它
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));
您可能想要从 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());
虽然我会将它分配给中间变量,因为这对我来说看起来更干净:
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());
我有一张包含信息的树状图。我可以通过它进行 foreach 并选择我想要打印的字段,或者只是从中创建一个 class。但我无法从中找出转换并将其放入 List<>
理想情况下,我希望它返回为
List<AssetMinuteTotals> listATM = assetsList.stream() ..
或者以另一种形式使 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()));
group
是这样定义的:
Comparator<Asset> group=Comparator.comparing(Asset::getPaper)
.thenComparing(Asset::getTradeMinutesSinceMidnight);
这个我试过了。但这不会编译。
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);
我可以通过将列表作为参数传递来解决这个问题。但这看起来很难看,因为我必须在 AssetMinuteTotals class
的构造函数中实现它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));
您可能想要从 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());
虽然我会将它分配给中间变量,因为这对我来说看起来更干净:
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());