java怎么分组查询_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 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值