JAVA8 分组统计

JAVA8使用stream()根据ID对List进行分组统计

Apple类:

/**
 * @version: V1.0
 * @author: fendo
 * @className: Apple
 * @packageName: com.xxx.xxxx.xxxx.xxxx
 * @description: 苹果
 * @data: 2018-06-11 11:15  
 **/
public class Apple {

    /**
     * 主键
     */
    private String id;

    /**
     * 名称
     */
    private String name;

    /**
     * 价格
     */
    private BigDecimal price;

    /**
     * 总数
     */
    private Long count;

    /**
     * 类别
     */
    private String type;

    public Apple() {

    }

    public Apple(String id, String name, BigDecimal price, Long count) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.count = count;
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public BigDecimal getPrice() {
        return price;
    }

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

    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

测试类:

    public static void main(String[] args) {

        List<Apple> appleList = Lists.newArrayList();
        Apple apple1 = new Apple();
        apple1.setId("1");
        apple1.setName("fendo1");
        apple1.setCount((long) 10);
        apple1.setType("1");
        apple1.setPrice(new BigDecimal(20));
        appleList.add(apple1);

        Apple apple2 = new Apple();
        apple2.setId("2");
        apple2.setName("fendo2");
        apple2.setCount((long) 10);
        apple2.setType("1");
        apple2.setPrice(new BigDecimal(20));
        appleList.add(apple2);

        Apple apple6 = new Apple();
        apple6.setId("6");
        apple6.setName("fendo6");
        apple6.setCount((long) 30);
        apple6.setType("1");
        apple6.setPrice(new BigDecimal(20));
        appleList.add(apple6);

        Apple apple3 = new Apple();
        apple3.setId("3");
        apple3.setName("fendo3");
        apple3.setCount((long) 10);
        apple3.setType("2");
        apple3.setPrice(new BigDecimal(20));
        appleList.add(apple3);

        Apple apple4 = new Apple();
        apple4.setId("4");
        apple4.setName("fendo4");
        apple4.setCount((long) 10);
        apple4.setType("3");
        apple4.setPrice(new BigDecimal(20));
        appleList.add(apple4);

        Apple apple5 = new Apple();
        apple5.setId("5");
        apple5.setName("fendo5");
        apple5.setCount((long) 10);
        apple5.setType("4");
        apple5.setPrice(new BigDecimal(20));
        appleList.add(apple5);

        //分组
        Map<String,List<Apple>> map = appleList.stream().collect(Collectors.groupingBy(Apple::getType));
        for (Map.Entry<String, List<Apple>> entry : map.entrySet()) {
            System.out.println("分组" + JsonUtil.toJson(entry));
        }

        //分组求和
        Map<String, LongSummaryStatistics> collect = appleList.stream().collect(
                Collectors.groupingBy(Apple::getType,
                        Collectors.summarizingLong(Apple::getCount)));
        for (Map.Entry<String, LongSummaryStatistics> entry : collect.entrySet()) {
            LongSummaryStatistics longSummaryStatistics = entry.getValue();
            System.out.println("----------------key----------------" + entry.getKey());
            System.out.println("求和:" + longSummaryStatistics.getSum());
            System.out.println("求平均" + longSummaryStatistics.getAverage());
            System.out.println("求最大:" + longSummaryStatistics.getMax());
            System.out.println("求最小:" + longSummaryStatistics.getMin());
            System.out.println("求总数:" + longSummaryStatistics.getCount());
        }

    }

输出如下:

分组
{
	"key": "1",
	"value": [{
		"id": "1",
		"name": "fendo1",
		"price": 20,
		"count": 10,
		"type": "1"
	}, {
		"id": "2",
		"name": "fendo2",
		"price": 20,
		"count": 10,
		"type": "1"
	}, {
		"id": "6",
		"name": "fendo6",
		"price": 20,
		"count": 30,
		"type": "1"
	}]
}
分组
{
	"key": "2",
	"value": [{
		"id": "3",
		"name": "fendo3",
		"price": 20,
		"count": 10,
		"type": "2"
	}]
}
分组
{
	"key": "3",
	"value": [{
		"id": "4",
		"name": "fendo4",
		"price": 20,
		"count": 10,
		"type": "3"
	}]
}
分组
{
	"key": "4",
	"value": [{
		"id": "5",
		"name": "fendo5",
		"price": 20,
		"count": 10,
		"type": "4"
	}]
}
----------------key----------------1
求和:50
求平均16.666666666666668
求最大:30
求最小:10
求总数:3
----------------key----------------2
求和:10
求平均10.0
求最大:10
求最小:10
求总数:1
----------------key----------------3
求和:10
求平均10.0
求最大:10
求最小:10
求总数:1
----------------key----------------4
求和:10
求平均10.0
求最大:10
求最小:10
求总数:1

    原文作者:lfendo
    原文地址: https://blog.csdn.net/u011781521/article/details/80651119
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞