亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用流添加持續時間的數組列表

使用流添加持續時間的數組列表

慕田峪9158850 2023-07-19 16:05:53
我有一個秒表類來測量生成報告所需的時間:public class Stopwatch {  public String report;  public Instant startTime;  public Instant endTime;  public Duration duration;}運行報告時,會收集時間和持續時間:private ArrayList<Stopwatch> _reportStats;最后我想知道所有報告的總持續時間,例如:Duration total = Duration.ZERO; // was null;for (Stopwatch s: _reportStats) {  total = total.plus(s.duration);}除了我想使用流和減少,但我無法得到正確的語法:Duration total = _reportStats.stream().reduce(... syntax ...);
查看完整描述

1 回答

?
米琪卡哇伊

TA貢獻1998條經驗 獲得超6個贊

您可以將sumreduce標識值 (Duration.ZERO ) 一起使用,并將其定義Duration.plus為累加器:

Duration total = _reportStats.stream()
        .map(Stopwatch::getDuration)
        .reduce(Duration.ZERO, Duration::plus);


查看完整回答
反對 回復 2023-07-19
  • 1 回答
  • 0 關注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號