[java]Stream API——reduce

來源:互聯網
上載者:User

標籤:operator   方法簽名   reduce   方法   元素   code   結果   apply   stream   

彙總操作reduce

T reduce(T identity, BinaryOperator accumulator)

代碼:

int value = Stream.of(1, 2, 3, 4).reduce(100, (sum, item) -> sum + item);

或者使用方法引用:

int value = Stream.of(1, 2, 3, 4).reduce(100, Integer::sum);

value結果:101,103,106,110

identity:它允許使用者提供一個迴圈計算的初始值。(100)

accumulator:計算的累加器,其方法簽名為apply(T t,U u),在該reduce方法中第一個參數t(sum)為上次Function Compute的傳回值,第二個參數u(item)為Stream中的元素,這個函數把這兩個值計算apply,得到的和會被賦值給下次執行這個方法的第一個參數

[java]Stream API——reduce

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.