java8 lambda Group分组示例

2024-10-09 08:38:34 浏览数 (2)

导入jar包 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency>

<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3.1</version> </dependency> model类

代码语言:javascript复制
import java.math.BigDecimal;


public class Product {
    Long id;
    Integer num;
    BigDecimal price;
    String name;
    String category;

    public Product(Long id, Integer num, BigDecimal price, String name, String category) {
        this.id = id;
        this.num = num;
        this.price = price;
        this.name = name;
        this.category = category;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }
}
代码语言:javascript复制
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.google.gson.Gson;
import org.junit.Test;
import org.springframework.boot.autoconfigure.gson.GsonBuilderCustomizer;
import org.springframework.boot.json.GsonJsonParser;
import org.springframework.http.converter.json.GsonBuilderUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * https://blog.csdn.net/u014231523/article/details/102535902
 */
public class GroupingByTest {

    @Test
    public void testGroupingByTest(){
        Product prod1 = new Product(1L, 1, new BigDecimal("15.5"), "面包", "零食");
        Product prod2 = new Product(2L, 2, new BigDecimal("20"), "饼干", "零食");
        Product prod3 = new Product(3L, 3, new BigDecimal("30"), "月饼", "零食");
        Product prod4 = new Product(4L, 3, new BigDecimal("10"), "青岛啤酒", "啤酒");
        Product prod5 = new Product(5L, 10, new BigDecimal("15"), "百威啤酒", "啤酒");
//        List<Product> prodList = Lists.newArrayList(prod1, prod2, prod3, prod4, prod5);
        List<Product> prodList = new ArrayList<Product>();
        prodList.add(prod1);
        prodList.add(prod2);
        prodList.add(prod3);
        prodList.add(prod4);
        prodList.add(prod5);


        Map<String, List<Product>> prodMap= prodList.stream().collect(Collectors.groupingBy(Product::getCategory));
        System.out.println("prodMap="   new Gson().toJson(prodMap));
        /**
         * prodMap={"啤酒":[{"id":4,"num":3,"price":10,"name":"青岛啤酒","category":"啤酒"},{"id":5,"num":10,"price":15,"name":"百威啤酒","category":"啤酒"}],
         * "零食":[{"id":1,"num":1,"price":15.5,"name":"面包","category":"零食"},{"id":2,"num":2,"price":20,"name":"饼干","category":"零食"},{"id":3,"num":3,"price":30,"name":"月饼","category":"零食"}]}
         */

        Map<String, List<Product>> prodMap2 = prodList.stream().collect(Collectors.groupingBy(item -> item.getCategory()   "_"   item.getName()));
        System.out.println("prodMap2="   new Gson().toJson(prodMap2));
        /**
         * prodMap2={"零食_月饼":[{"id":3,"num":3,"price":30,"name":"月饼","category":"零食"}],
         * "零食_面包":[{"id":1,"num":1,"price":15.5,"name":"面包","category":"零食"}],
         * "啤酒_百威啤酒":[{"id":5,"num":10,"price":15,"name":"百威啤酒","category":"啤酒"}],
         * "啤酒_青岛啤酒":[{"id":4,"num":3,"price":10,"name":"青岛啤酒","category":"啤酒"}],
         * "零食_饼干":[{"id":2,"num":2,"price":20,"name":"饼干","category":"零食"}]}
         */

        Map<String, List<Product>> prodMap3= prodList.stream().collect(Collectors.groupingBy(item -> {
            if(item.getNum() < 3) {
                return "3";
            }else {
                return "other";
            }
        }));
        System.out.println("prodMap3="   new Gson().toJson(prodMap3));
        /**
         * prodMap3={"other":[{"id":3,"num":3,"price":30,"name":"月饼","category":"零食"},
         * {"id":4,"num":3,"price":10,"name":"青岛啤酒","category":"啤酒"},
         * {"id":5,"num":10,"price":15,"name":"百威啤酒","category":"啤酒"}],
         * "3":[{"id":1,"num":1,"price":15.5,"name":"面包","category":"零食"},{"id":2,"num":2,"price":20,"name":"饼干","category":"零食"}]}
         */

        Map<String, Map<String, List<Product>>> prodMap4= prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.groupingBy(item -> {
            if(item.getNum() < 3) {
                return "3";
            }else {
                return "other";
            }
        })));
        System.out.println("prodMap4="   new Gson().toJson(prodMap4));
        /**
         * prodMap4={"啤酒":{"other":[{"id":4,"num":3,"price":10,"name":"青岛啤酒","category":"啤酒"},{"id":5,"num":10,"price":15,"name":"百威啤酒","category":"啤酒"}]},
         * "零食":{"other":[{"id":3,"num":3,"price":30,"name":"月饼","category":"零食"}],
         * "3":[{"id":1,"num":1,"price":15.5,"name":"面包","category":"零食"},{"id":2,"num":2,"price":20,"name":"饼干","category":"零食"}]}}
         */

        Map<String, Long> prodMap5 = prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.counting()));
        System.out.println("prodMap5="   new Gson().toJson(prodMap5));
        /**
         * prodMap5={"啤酒":2,"零食":3}
         */

        Map<String, Integer> prodMap6 = prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.summingInt(Product::getNum)));
        System.out.println("prodMap6="   new Gson().toJson(prodMap6));
        /**
         * prodMap6={"啤酒":13,"零食":6}
         */

        Map<String, Product> prodMap7 = prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparingInt(Product::getNum)), Optional::get)));
        System.out.println("prodMap7="   new Gson().toJson(prodMap7));
        /**
         * prodMap7={"啤酒":{"id":5,"num":10,"price":15,"name":"百威啤酒","category":"啤酒"},
         * "零食":{"id":3,"num":3,"price":30,"name":"月饼","category":"零食"}}
         */

        Map<String, Set<String>> prodMap8 = prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.mapping(Product::getName, Collectors.toSet())));
        System.out.println("prodMap8="   new Gson().toJson(prodMap8));
        /**
         * prodMap8={"啤酒":["青岛啤酒","百威啤酒"],"零食":["面包","饼干","月饼"]}
         */

    }
}

0 人点赞