欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java8?Stream流多字段求和、匯聚的實(shí)例

 更新時(shí)間:2022年05月06日 11:35:13   作者:我想寫(xiě)游戲  
這篇文章主要介紹了Java8?Stream流多字段求和、匯聚的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Stream流多字段求和、匯聚

實(shí)現(xiàn)方法

利用

Collectors.toMap(Function keyMapper, Function valueMapper, BinaryOperator mergeFunction)
  • keyMapper:代表你最終想要獲得的Map<Key, Value> 的Key
  • valueMapper:代表你最終想要獲得的Map<Key, Value> 的Value
  • mergeFunction:表示碰到Key沖突是處理過(guò)程,{x, y}中x是已匯聚對(duì)象,y表示當(dāng)前處理對(duì)象

對(duì)象類(lèi)型數(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ù)式編程,最終目的是為了求和,類(lèi)似反射,具體使用方式請(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類(lèi)型數(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分組求和使用筆記

話(huà)不多說(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("-----------這里是分界線(xiàn)-----------------------------");
? ? ? ? list2.forEach(e -> System.out.println(e.getCode() + ":" + e.getCount()));
? ? }

輸出結(jié)果:

1:2
-----------這里是分界線(xiàn)-----------------------------
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ù)暮糜?,Mark一下,歡迎大神在評(píng)論區(qū)留下你的Stream騷操作。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 教你如何使用Java輸出各種形狀

    教你如何使用Java輸出各種形狀

    本文小編將向大家介紹的是如何利用Java輸出各種不同的形狀,本文一共介紹了七種有趣的形狀,感興趣的小伙伴趕快收藏起來(lái)吧
    2021-09-09
  • Java中Integer類(lèi)型值相等判斷方法

    Java中Integer類(lèi)型值相等判斷方法

    這篇文章主要給大家介紹了關(guān)于Java中Integer類(lèi)型值相等判斷的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Java開(kāi)發(fā)中的OOM內(nèi)存溢出問(wèn)題詳解

    Java開(kāi)發(fā)中的OOM內(nèi)存溢出問(wèn)題詳解

    這篇文章主要介紹了Java開(kāi)發(fā)中的OOM內(nèi)存溢出問(wèn)題詳解,OOM,全稱(chēng)?Out?Of?Memory,意思是內(nèi)存耗盡或內(nèi)存溢出,當(dāng)JVM因?yàn)闆](méi)有足夠的內(nèi)存來(lái)為對(duì)象分配空間并且垃圾回收器也已經(jīng)沒(méi)有空間可回收時(shí),就會(huì)拋出這個(gè)?error,需要的朋友可以參考下
    2023-08-08
  • Java中典型的內(nèi)存泄露問(wèn)題和解決方法

    Java中典型的內(nèi)存泄露問(wèn)題和解決方法

    這篇文章主要介紹了Java中典型的內(nèi)存泄露問(wèn)題和解決方法,典型的內(nèi)存泄露例子是一個(gè)沒(méi)有實(shí)現(xiàn)hasCode和 equals方法的Key類(lèi)在HashMap中保存的情況,可以通過(guò)實(shí)現(xiàn)Key類(lèi)的equals和hasCode方法解決這種內(nèi)存泄漏問(wèn)題,需要的朋友可以參考下
    2014-04-04
  • 在Java的Struts框架下進(jìn)行web編程的入門(mén)教程

    在Java的Struts框架下進(jìn)行web編程的入門(mén)教程

    這篇文章主要介紹了在Java的Struts框架下進(jìn)行web編程的入門(mén)教程,需要的朋友可以參考下
    2015-11-11
  • SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行代碼的幾種實(shí)現(xiàn)方式

    SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行代碼的幾種實(shí)現(xiàn)方式

    這篇文章主要給大家介紹了關(guān)于SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行代碼的幾種實(shí)現(xiàn)方式,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-02-02
  • Springboot使用RabbitMQ實(shí)現(xiàn)關(guān)閉超時(shí)訂單(示例詳解)

    Springboot使用RabbitMQ實(shí)現(xiàn)關(guān)閉超時(shí)訂單(示例詳解)

    介紹了如何在Spring Boot項(xiàng)目中使用RabbitMQ實(shí)現(xiàn)訂單的延時(shí)處理和超時(shí)關(guān)閉,通過(guò)配置RabbitMQ的交換機(jī)、隊(duì)列和綁定關(guān)系,以及編寫(xiě)監(jiān)聽(tīng)方法,實(shí)現(xiàn)了訂單數(shù)據(jù)的發(fā)送和延時(shí)消費(fèi),感興趣的朋友一起看看吧
    2025-01-01
  • 基于SpringBoot?使用?Flink?收發(fā)Kafka消息的示例詳解

    基于SpringBoot?使用?Flink?收發(fā)Kafka消息的示例詳解

    這篇文章主要介紹了基于SpringBoot?使用?Flink?收發(fā)Kafka消息,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-01-01
  • Java中ReUtil正則表達(dá)式工具庫(kù)的使用

    Java中ReUtil正則表達(dá)式工具庫(kù)的使用

    ReUtil是Hutool庫(kù)中的正則表達(dá)式工具類(lèi),提供了多種常用正則表達(dá)式操作方法,下面就來(lái)介紹一下ReUtil的使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-02-02
  • Java高效映射工具M(jìn)apStruct的使用示例

    Java高效映射工具M(jìn)apStruct的使用示例

    MapStruct 是一個(gè) Java 注解處理器,用于在不同 Java Beans 或數(shù)據(jù)傳輸對(duì)象(DTOs)之間自動(dòng)生成類(lèi)型安全的映射代碼,這是一個(gè)編譯時(shí)映射框架,意味著它利用注解在編譯時(shí)生成代碼,本文將給大家介紹一下Java注解處理器MapStruct的使用示例,需要的朋友可以參考下
    2023-12-12

最新評(píng)論