JAVA8分组统计JAVA8使⽤stream()根据ID对List进⾏分组统计
Apple类:
/**
* @version: V1.0
* @author: fendo
* @className: Apple
* @packageName:
* @description: 苹果
* @data: 2018-06-11 11:15
**/
public class Apple {
/
**
* 主键
*/
private String id;
/**
* 名称
*/
private String name;
苹果apple id注册
/**
* 价格
*/
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;
}
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) {
}
public String getType() {
return type;
}
public void setType(String type) {
}
}
测试类:
public static void main(String[] args) {
List<Apple> appleList = wArrayList();        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));
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().upingBy(Apple::getType));        for (Map.Entry<String, List<Apple>> entry : Set()) {
System.out.println("分组" + Json(entry));
}
//分组求和
Map<String, LongSummaryStatistics> collect = appleList.stream().collect(
Collectors.summarizingLong(Apple::getCount)));
for (Map.Entry<String, LongSummaryStatistics> entry : Set()) {
LongSummaryStatistics longSummaryStatistics = Value();
System.out.println("----------------key----------------" + Key());
System.out.println("求和:" + Sum());
System.out.println("求平均" + Average());
System.out.println("求最⼤:" + Max());
System.out.println("求最⼩:" + Min());
System.out.println("求总数:" + Count());
}
}
输出如下:
分组
{
"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