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 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> map = appleList.stream().collect(Collectors.groupingBy(Apple::getType));

for (Map.Entry> entry : map.entrySet()) {

System.out.println("分组" + JsonUtil.toJson(entry));

}

//分组求和

Map collect = appleList.stream().collect(

Collectors.groupingBy(Apple::getType,

Collectors.summarizingLong(Apple::getCount)));

for (Map.Entry 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

java8分组_JAVA8 分组统计相关推荐

  1. java 多字段分组_java8 stream统计、汇总、多字段分组、多个列汇总统计

    前言 本文将展示groupingBy收集器的多个示例,阅读本文需要先准备Java Stream和Java收集器Collector的知识. 一.GroupingBy收集器 Java8的Stream AP ...

  2. java8三次分组_Java8分组(groupingBy)

    1.分组,计数,排序 public class Java8Example1 { public static void main(String[] args) { List items = Arrays ...

  3. pandas使用groupby函数和describe函数计算不同分组的描述性统计(descriptive statistics of each group in dataframe)

    pandas使用groupby函数和describe函数计算不同分组的描述性统计(descriptive statistics of each group in dataframe) 目录

  4. R语言ggpubr包ggsummarystats函数可视化分组条形图(自定义分组颜色、添加抖动数据点jitter、误差条)并在X轴标签下方添加分组对应的统计值(样本数N、中位数、四分位数的间距iqr)

    R语言ggpubr包ggsummarystats函数可视化分组条形图(自定义分组颜色.添加抖动数据点jitter.误差条error bar)并在X轴标签下方添加分组对应的统计值(样本数N.中位数med ...

  5. R语言编写自定义函数基于ggsumarystats函数计算每个分组的统计值、自定义可视化分组分面条形图,并在X轴标签下方添加分组对应的统计值(样本数N、中位数median、四分位数的间距iqr)

    R语言编写自定义函数基于ggsumarystats函数计算每个分组的统计值.自定义可视化分组分面条形图,并在X轴标签下方添加分组对应的统计值(样本数N.中位数median.四分位数的间距iqr) 目录

  6. R语言编写自定义分组统计函数(customize statistics function)可视化分组箱图并在X轴标签下方添加分组对应的统计值(样本数N、中位数median、四分位数的间距iqr)

    R语言编写自定义分组统计函数(customize statistics function)可视化分组箱图并在X轴标签下方添加分组对应的统计值(样本数N.中位数median.四分位数的间距iqr) 目录

  7. 数据库实验6 数据库的分组查询和统计查询

    实验6 数据库的分组查询和统计查询 6.1实验目的及要求 熟练掌握数据查询中的分组.统计.计算和集合的操作方法 6.2实验内容 使用聚集函数查询.分组计算查询.集合查询 6.3实验步骤 6.3.1使用 ...

  8. oracle多条件分组查询数量_ORACLE分组查询和统计等

    select flow_id,rw from (select t.flow_id ,rownum as rw from apex_030200.wwv_flow_list_templates t)  ...

  9. pandas学习(数据分组与分组运算、离散化处理、数据合并)

    pandas学习(数据分组与分组运算.离散化处理.数据合并) 目录 数据分组与分组运算 离散化处理 数据合并 数据分组与分组运算 GroupBy技术:实现数据的分组,和分组运算,作用类似于数据透视表数 ...

最新文章

  1. change python是什么意思_回溯是什么意思?
  2. 皮一皮:确定不是某种特色酒店?
  3. SpringMVC中,前台jsp封装参数,绑定参数,传递参数到后台controller的过程详解
  4. SkinRibbonGalleryBarItem添加无Item问题
  5. 【运营】“顶级运营”最强速成攻略!目前国内不超过200人!
  6. SSM中使用Druid连接池
  7. linux下查看cpu,内存,硬盘等硬件信息的方法
  8. MyBatis入门(一) -- 简介
  9. 常州模拟赛d3t2 灰狼呼唤着同胞
  10. 越智能?越危险?技术到底应不应该进步?
  11. sci的figure怎么做_SCI论文中Figure图如何制作?
  12. Java 线程死锁初理解
  13. SAXReader解析器
  14. 动视是否磨灭了暴雪的灵魂?
  15. Faceless —— 开源的匿名社交应用
  16. python:numpy从数组中随机抽取元素
  17. 员工满意度模型定期检讨工作规范有哪些?
  18. NLP入门(六)pyltp的介绍与使用
  19. frameset框架的简单使用
  20. 手机快播的多!mv文件合并软件工具

热门文章

  1. Nest.js模块机制的概念和实现原理
  2. 北漂9年了,37岁的我跳槽进了国企,开启了一段新的人生旅程
  3. 一键加群android代码如何使用!
  4. 操作系统中的信号量及P、V操作
  5. Android音频可视化
  6. 浏览器控制bartender打印方案
  7. php notice 影响,PHP notice/warning 对性能的影响
  8. 关机、睡眠和休眠有啥区别
  9. google有自定义文章流畅度的伪原创工具吗
  10. 电脑只能上微信QQ不能浏览网页(解决方法)