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

痛定思痛。 2023-10-10 13:56 118阅读 0赞

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

核心功能代码片段

  1. //分组求和
  2. Map<String, LongSummaryStatistics> collect = list.stream().collect(
  3. Collectors.groupingBy(Fruit::getType,
  4. Collectors.summarizingLong(Fruit::getTotal)));
  5. for (Map.Entry<String, LongSummaryStatistics> entry : collect.entrySet()) {
  6. LongSummaryStatistics longSummaryStatistics = entry.getValue();
  7. System.out.println("----------------key----------------" + entry.getKey());
  8. System.out.println("求和:" + longSummaryStatistics.getSum());
  9. System.out.println("求平均" + longSummaryStatistics.getAverage());
  10. System.out.println("求最大:" + longSummaryStatistics.getMax());
  11. System.out.println("求最小:" + longSummaryStatistics.getMin());
  12. System.out.println("求总数:" + longSummaryStatistics.getCount());
  13. }

演示功能代码

  1. package com.zzg.test;
  2. import java.math.BigDecimal;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.LongSummaryStatistics;
  6. import java.util.Map;
  7. import java.util.stream.Collectors;
  8. import cn.hutool.json.JSONUtil;
  9. /**
  10. * 基于Java8 分组再统计
  11. * @author zzg
  12. *
  13. */
  14. public class GroupByStatissticsTest {
  15. static List<Fruit> initDate(){
  16. List<Fruit> list = new ArrayList<Fruit>();
  17. Fruit one = new Fruit();
  18. one.setName("苹果一级");
  19. one.setSid("1");
  20. one.setPrice(new BigDecimal("123456.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
  21. one.setTotal(1100L);
  22. one.setType("1");
  23. Fruit two = new Fruit();
  24. two.setName("苹果二级");
  25. two.setSid("2");
  26. two.setPrice(new BigDecimal("123546.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
  27. two.setTotal(89L);
  28. two.setType("1");
  29. Fruit three = new Fruit();
  30. three.setName("苹果三级");
  31. three.setSid("3");
  32. three.setPrice(new BigDecimal("987.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
  33. three.setTotal(1039L);
  34. three.setType("1");
  35. Fruit four = new Fruit();
  36. four.setName("梨子一级");
  37. four.setSid("4");
  38. four.setPrice(new BigDecimal("97.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
  39. four.setTotal(39L);
  40. four.setType("2");
  41. Fruit five = new Fruit();
  42. five.setName("梨子二级");
  43. five.setSid("5");
  44. five.setPrice(new BigDecimal("970.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
  45. five.setTotal(399L);
  46. five.setType("2");
  47. Fruit six = new Fruit();
  48. six.setName("西瓜一级");
  49. six.setSid("6");
  50. six.setPrice(new BigDecimal("1970.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
  51. six.setTotal(2399L);
  52. six.setType("3");
  53. list.add(one);
  54. list.add(two);
  55. list.add(three);
  56. list.add(four);
  57. list.add(five);
  58. list.add(six);
  59. return list;
  60. }
  61. public static void main(String[] args) {
  62. // TODO Auto-generated method stub
  63. List<Fruit> list = initDate();
  64. //分组
  65. Map<String,List<Fruit>> map = list.stream().collect(Collectors.groupingBy(Fruit::getType));
  66. for (Map.Entry<String, List<Fruit>> entry : map.entrySet()) {
  67. System.out.println("分组" + JSONUtil.toJsonStr(entry));
  68. }
  69. //分组求和
  70. Map<String, LongSummaryStatistics> collect = list.stream().collect(
  71. Collectors.groupingBy(Fruit::getType,
  72. Collectors.summarizingLong(Fruit::getTotal)));
  73. for (Map.Entry<String, LongSummaryStatistics> entry : collect.entrySet()) {
  74. LongSummaryStatistics longSummaryStatistics = entry.getValue();
  75. System.out.println("----------------key----------------" + entry.getKey());
  76. System.out.println("求和:" + longSummaryStatistics.getSum());
  77. System.out.println("求平均" + longSummaryStatistics.getAverage());
  78. System.out.println("求最大:" + longSummaryStatistics.getMax());
  79. System.out.println("求最小:" + longSummaryStatistics.getMin());
  80. System.out.println("求总数:" + longSummaryStatistics.getCount());
  81. }
  82. }
  83. static class Fruit{
  84. private String sid;
  85. private String name;
  86. private String type;
  87. private Long total;
  88. private BigDecimal price;
  89. public String getSid() {
  90. return sid;
  91. }
  92. public void setSid(String sid) {
  93. this.sid = sid;
  94. }
  95. public String getName() {
  96. return name;
  97. }
  98. public void setName(String name) {
  99. this.name = name;
  100. }
  101. public String getType() {
  102. return type;
  103. }
  104. public void setType(String type) {
  105. this.type = type;
  106. }
  107. public Long getTotal() {
  108. return total;
  109. }
  110. public void setTotal(Long total) {
  111. this.total = total;
  112. }
  113. public BigDecimal getPrice() {
  114. return price;
  115. }
  116. public void setPrice(BigDecimal price) {
  117. this.price = price;
  118. }
  119. }
  120. }

效果截图

watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBA5Zyo5aWL5paX55qE5aSn6YGT_size_20_color_FFFFFF_t_70_g_se_x_16

发表评论

表情:
评论列表 (有 0 条评论,118人围观)

还没有评论,来说两句吧...

相关阅读