Java8?Stream流多字段求和、匯聚的實(shí)例
Stream流多字段求和、匯聚
實(shí)現(xiàn)方法
利用
Collectors.toMap(Function keyMapper, Function valueMapper, BinaryOperator mergeFunction)
keyMapper
:代表你最終想要獲得的Map<Key, Value> 的KeyvalueMapper
:代表你最終想要獲得的Map<Key, Value> 的ValuemergeFunction
:表示碰到Key沖突是處理過程,{x, y}中x是已匯聚對(duì)象,y表示當(dāng)前處理對(duì)象
對(duì)象類型數(shù)據(jù)處理
public static Map<String, Model> streamGroupSum(List<Model> datas){ ? ? return datas.stream().collect(Collectors.toMap(k -> k.getCode(), v -> v, (x, y) -> x.addCount().addAll(y))); ? }
Model
@Data class Model{ private String code; private int count = 0; private Integer sum1; private Integer sum2; public Model(String code, Integer sum1, Integer sum2){ this.code = code; this.sum1 = sum1; this.sum2 = sum2; } public Model addCount(){ this.count++; return this; } public Model addAll(Model y){ return add(Model::setSum1, Model::getSum1, y) .add(Model::setSum2, Model::getSum2, y); } /** * 使用函數(shù)式編程,最終目的是為了求和,類似反射,具體使用方式請(qǐng)移步函數(shù)式編程 */ public Model add(BiConsumer<Model, Integer> set, Function<Model, Integer> get, Model y){ set.accept(this, get.apply(this) + get.apply(y)); return this; } }
Map類型數(shù)據(jù)處理
public static void main (String[] args) { List<Map<String, Object>> datas = getDatas(); streamMapSum(datas); } public static Map<Object, Map<String, Object>> streamMapSum (List<Map<String, Object>> datas) { return datas.stream() .collect(Collectors.toMap(k -> k.get("name"), v -> { v.put("count", 1); return v; } , (x, y) -> { x.put("count", (int) x.get("count") + 1); x.put("aaa", (int) x.get("aaa") + (int) y.get("aaa")); x.put("bbb", (int) x.get("bbb") + (int) y.get("bbb")); x.put("ccc", (int) x.get("ccc") + (int) y.get("ccc")); return x; /* //使用ofMap重構(gòu) return ofMap("name", x.get("name") , "count", (int) x.get("count") + 1 , "aaa", add(x, y, "aaa") , "bbb", add(x, y, "bbb") , "ccc", add(x, y, "ccc"));*/ } ) ); } public static int add (Map<String, Object> x, Map<String, Object> y, String key) { return (int) x.get(key) + (int) y.get(key); } public static Map<String, Object> ofMap (Object... objs) { System.out.println("ofMap"); Map<String, Object> map = new LinkedHashMap<>(); for (int i = 0; i < objs.length; i = i + 2) { map.put(objs[i].toString(), objs[i + 1]); } return map; } public static List<Map<String, Object>> getDatas () { List<Map<String, Object>> list = new ArrayList<>(); list.add(ofMap("name", "張三", "aaa", 3, "bbb", 5, "ccc", 6)); list.add(ofMap("name", "張三", "aaa", 8, "bbb", 51, "ccc", 521)); list.add(ofMap("name", "李四", "aaa", 9, "bbb", 53, "ccc", 23)); return list; }
Stream分組求和使用筆記
話不多說(shuō),直接貼代碼,分組使用
class Foo { ? ? private int code; ? ? private int count; ? ? public Foo(int code, int count) { ? ? ? ? this.code = code; ? ? ? ? this.count = count; ? ? } ? ? public int getCode() { ? ? ? ? return code; ? ? } ? ? public void setCode(int code) { ? ? ? ? this.code = code; ? ? } ? ? public int getCount() { ? ? ? ? return count; ? ? } ? ? public void setCount(int count) { ? ? ? ? this.count = count; ? ? } }
public static void main(String[] args) { ? ? ? ? Foo foo1 = new Foo(1, 2); ? ? ? ? Foo foo2 = new Foo(2, 23); ? ? ? ? Foo foo3 = new Foo(2, 6); ? ? ? ? List<Foo> list = new ArrayList<>(4); ? ? ? ? list.add(foo1); ? ? ? ? list.add(foo2); ? ? ? ? list.add(foo3); ? ? ? ? Map<Integer, List<Foo>> collect = list.stream().collect(Collectors.groupingBy(Foo::getCode)); ? ? ? ? List<Foo> list1 = collect.get(1); ? ? ? ? List<Foo> list2 = collect.get(2); ? ? ? ? list1.forEach(e -> System.out.println(e.getCode() + ":" + e.getCount())); ? ? ? ? System.out.println("-----------這里是分界線-----------------------------"); ? ? ? ? list2.forEach(e -> System.out.println(e.getCode() + ":" + e.getCount())); ? ? }
輸出結(jié)果:
1:2
-----------這里是分界線-----------------------------
2:23
2:6
分組求和使用
public static void main(String[] args) { ? ? ? ? Foo foo1 = new Foo(1, 2); ? ? ? ? Foo foo2 = new Foo(2, 23); ? ? ? ? Foo foo3 = new Foo(2, 6); ? ? ? ? List<Foo> list = new ArrayList<>(4); ? ? ? ? list.add(foo1); ? ? ? ? list.add(foo2); ? ? ? ? list.add(foo3); ? ? ? ? Map<Integer, IntSummaryStatistics> collect = list.stream().collect(Collectors.groupingBy(Foo::getCode, Collectors.summarizingInt(Foo::getCount))); ? ? ? ? IntSummaryStatistics statistics1 = collect.get(1); ? ? ? ? IntSummaryStatistics statistics2 = collect.get(2); ? ? ? ? System.out.println(statistics1.getSum()); ? ? ? ? System.out.println(statistics1.getAverage()); ? ? ? ? System.out.println(statistics1.getMax()); ? ? ? ? System.out.println(statistics1.getMin()); ? ? ? ? System.out.println(statistics1.getCount()); ? ? ? ? System.out.println(statistics2.getSum()); ? ? ? ? System.out.println(statistics2.getAverage()); ? ? ? ? System.out.println(statistics2.getMax()); ? ? ? ? System.out.println(statistics2.getMin()); ? ? ? ? System.out.println(statistics2.getCount()); ? ? }
輸出結(jié)果:
2
2.0
2
2
1
29
14.5
23
6
2
stream真的是相當(dāng)?shù)暮糜茫琈ark一下,歡迎大神在評(píng)論區(qū)留下你的Stream騷操作。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺談springboot一個(gè)service內(nèi)組件的加載順序
這篇文章主要介紹了springboot一個(gè)service內(nèi)組件的加載順序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家2021-08-08淺談virtual、abstract方法和靜態(tài)方法、靜態(tài)變量理解
下面小編就為大家?guī)?lái)一篇淺談virtual、abstract方法和靜態(tài)方法、靜態(tài)變量理解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-02-02spring cloud 使用Eureka 進(jìn)行服務(wù)治理方法
這篇文章主要介紹了spring cloud 使用Eureka 進(jìn)行服務(wù)治理方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-05-05Java并發(fā)工具之CyclicBarrier使用詳解
這篇文章主要介紹了Java并發(fā)工具之CyclicBarrier使用詳解,CyclicBarrier是一個(gè)同步器,允許一組線程相互之間等待,直到到達(dá)某個(gè)公共屏障點(diǎn)(common barrier point),再繼續(xù)執(zhí)行,需要的朋友可以參考下2023-12-12SpringBoot集成POI實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出的示例詳解
Apache?POI?是用Java編寫的免費(fèi)開源的跨平臺(tái)的?Java?API,Apache?POI提供API給Java程序?qū)icrosoft?Office格式檔案讀和寫的功能。本文主要介紹通過SpringBoot集成POI工具實(shí)現(xiàn)Excel的導(dǎo)入和導(dǎo)出功能,需要的可以參考一下2022-07-07Java并發(fā)編程深入理解之Synchronized的使用及底層原理詳解 上
在并發(fā)編程中存在線程安全問題,主要原因有:1.存在共享數(shù)據(jù) 2.多線程共同操作共享數(shù)據(jù)。關(guān)鍵字synchronized可以保證在同一時(shí)刻,只有一個(gè)線程可以執(zhí)行某個(gè)方法或某個(gè)代碼塊,同時(shí)synchronized可以保證一個(gè)線程的變化可見(可見性),即可以代替volatile2021-09-09