Java8 Stream针对List先分组再求和、最大值、最小值、平均值等

解题思路:JAVA8使用stream()根据类型对List进行分组统计。

核心功能代码片段

 //分组求和
        Map<String, LongSummaryStatistics> collect = list.stream().collect(
                Collectors.groupingBy(Fruit::getType,
                        Collectors.summarizingLong(Fruit::getTotal)));
        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());
        }

演示功能代码

package com.zzg.test;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.json.JSONUtil;

/**
 * 基于Java8 分组再统计
 * @author zzg
 *
 */
public class GroupByStatissticsTest {
	
	static List<Fruit> initDate(){
		List<Fruit> list = new ArrayList<Fruit>();
		
		Fruit one = new Fruit();
		one.setName("苹果一级");
		one.setSid("1");
		one.setPrice(new BigDecimal("123456.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
		one.setTotal(1100L);
		one.setType("1");
		
		
		Fruit two = new Fruit();
		two.setName("苹果二级");
		two.setSid("2");
		two.setPrice(new BigDecimal("123546.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
		two.setTotal(89L);
		two.setType("1");
		
		Fruit three = new Fruit();
		three.setName("苹果三级");
		three.setSid("3");
		three.setPrice(new BigDecimal("987.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
		three.setTotal(1039L);
		three.setType("1");
		
		Fruit four = new Fruit();
		four.setName("梨子一级");
		four.setSid("4");
		four.setPrice(new BigDecimal("97.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
		four.setTotal(39L);
		four.setType("2");
		
		Fruit five = new Fruit();
		five.setName("梨子二级");
		five.setSid("5");
		five.setPrice(new BigDecimal("970.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
		five.setTotal(399L);
		five.setType("2");
		
		Fruit six = new Fruit();
		six.setName("西瓜一级");
		six.setSid("6");
		six.setPrice(new BigDecimal("1970.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
		six.setTotal(2399L);
		six.setType("3");
		
		list.add(one);
		list.add(two);
		list.add(three);
		list.add(four);
		list.add(five);
		list.add(six);
		return list;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<Fruit> list = initDate();
		
		  //分组
        Map<String,List<Fruit>> map = list.stream().collect(Collectors.groupingBy(Fruit::getType));
        for (Map.Entry<String, List<Fruit>> entry : map.entrySet()) {
            System.out.println("分组" + JSONUtil.toJsonStr(entry));
        }
        
      //分组求和
        Map<String, LongSummaryStatistics> collect = list.stream().collect(
                Collectors.groupingBy(Fruit::getType,
                        Collectors.summarizingLong(Fruit::getTotal)));
        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());
        }


	}
	
	static class Fruit{
		private String sid;
		private String name;
		private String type;
		private Long total;
		private BigDecimal price;
		public String getSid() {
			return sid;
		}
		public void setSid(String sid) {
			this.sid = sid;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getType() {
			return type;
		}
		public void setType(String type) {
			this.type = type;
		}
		public Long getTotal() {
			return total;
		}
		public void setTotal(Long total) {
			this.total = total;
		}
		public BigDecimal getPrice() {
			return price;
		}
		public void setPrice(BigDecimal price) {
			this.price = price;
		}
		
		
		
	}

}

效果截图

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值