我正在使用IntSummaryStatistics类来计算出我的类的统计数据。我寻找了三种计算统计的特殊方法。这是我的代码:
IntSummaryStatistics stats1 = orderEntries.stream()
.mapToInt((x) -> x.getAmount()).summaryStatistics();
IntSummaryStatistics stats2 = orderEntries.stream().collect(
Collectors.summarizingInt(o -> o.getAmount()));
IntSummaryStatistics istats2 = orderEntries.stream().
collect(
() -> new IntSummaryStatistics(),
(i,o) -> i.accept(o.getAmount()),
(i1, i2) -> i1.combine(i2));
IntSummaryStatistics istats = IntStream.of(51,22,50,27,35).
collect(IntSummaryStatistics::new, IntSummaryStatistics::accept,
IntSummaryStatistics::combine);哪一种方法更好?我们更喜欢哪一种?
发布于 2016-12-06 08:10:32
我会选择:
IntSummaryStatistics stats = orderEntries
.stream()
.collect(Collectors.summarizingInt(OrderEntry::getAmount));这一备选方案:
IntSummaryStatistics istats = IntStream.of(51,22,50,27,35).
collect(IntSummaryStatistics::new, IntSummaryStatistics::accept,
IntSummaryStatistics::combine);最糟糕的是,这正是IntStream.summaryStatistics所做的,只是显式地编写。所以第一种选择没有优势。
我将使用稍微修改的第二个选项,因为从我的角度来看,收集器更好地表示业务操作“订单输入金额摘要”。
https://stackoverflow.com/questions/40989955
复制相似问题