Lambda方法總結

toList                把流中所有元素收集到List中
示例:
Menu.getMenus.stream().collect(Collectors.toList())

toSet                把流中所有元素收集到Set中,刪除重複項
示例:Menu.getMenus.stream().collect(Collectors.toSet())

toCollection         把流中所有元素收集到給定的供應源創建的集合中
示例:Menu.getMenus.stream().collect(Collectors.toCollection(ArrayList::new))

Counting        Long         計算流中元素個數
示例:Long count=Menu.getMenus.stream().collect(counting);

SummingInt         Integer         對流中元素的一個整數屬性求和
示例:Integer count=Menu.getMenus.stream().collect(summingInt(Menu::getCalories))

averaging       Int Double 計算流中元素integer屬性的平均值
示例:Double averaging=Menu.getMenus.stream().collect(averagingInt(Menu::getCalories))

Joining         String        連接流中每個元素的toString方法生成的字符串
示例:String name=Menu.getMenus.stream().map(Menu::getName).collect(joining(“, ”))

maxBy         Optional         一個包裹了流中按照給定比較器選出的最大元素的optional,如果爲空返回的是Optional.empty()
示例:Optional fattest=Menu.getMenus.stream().collect(maxBy(Menu::getCalories))

minBy        Optional        一個包裹了流中按照給定比較器選出的最大元素的optional如果爲空返回的是Optional.empty()
示例: Menu.getMenus.stream().collect(minBy(Menu::getCalories))

Reducing         歸約操作產生的類型         從一個作爲累加器的初始值開始,利用binaryOperator與流中的元素逐個結合,從而將流歸約爲單個值
示例:int count=Menu.getMenus.stream().collect(reducing(0,Menu::getCalories,Integer::sum));

collectingAndThen         轉換函數返回的類型         包裹另一個轉換器,對其結果應用轉換函數
示例:Int count=Menu.getMenus.stream().collect(collectingAndThen(toList(),List::size))

groupingBy         Map<K,List>         根據流中元素的某個值對流中的元素進行分組,並將屬性值做爲結果map的鍵
示例:Menu.getMenus.stream().collect(groupingby(Menu::getType))

partitioningBy        Map<Boolean,List>         根據流中每個元素應用謂語的結果來對項目進行分區
示例: Menu.getMenus.stream().collect(partitioningBy(Menu::isType));

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章