java8分组_Java8分组(groupingBy)

1、分组,计数,排序

48304ba5e6f9fe08f3fa1abda7d326ab.png

public class Java8Example1 {

public static void main(String[] args) {

List items = Arrays.asList(

"apple", "apple",

"orange", "orange", "orange",

"blueberry",

"peach", "peach", "peach", "peach"

);

// 分组,计数

Map result = items.stream()

.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

System.out.println(result);

Map finalMap = new LinkedHashMap<>();

// 排序

result.entrySet().stream()

.sorted(Map.Entry.comparingByValue().reversed())

.forEachOrdered(e -> finalMap.put(e.getKey(), e.getValue()));

System.out.println(finalMap);

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

2、以下例子使用的类

48304ba5e6f9fe08f3fa1abda7d326ab.png

public class Item {

private String name;

private int qty;

private BigDecimal price;

public Item() {

}

public Item(String name, int qty, BigDecimal price) {

this.name = name;

this.qty = qty;

this.price = price;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getQty() {

return qty;

}

public void setQty(int qty) {

this.qty = qty;

}

public BigDecimal getPrice() {

return price;

}

public void setPrice(BigDecimal price) {

this.price = price;

}

@Override

public String toString() {

return "Item{" +

"name='" + name + '\'' +

", qty=" + qty +

", price=" + price +

'}';

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

3、分组,计数,计算数量

48304ba5e6f9fe08f3fa1abda7d326ab.png

public class Java8Example2 {

public static void main(String[] args) {

List items = Arrays.asList(

new Item("apple", 10, new BigDecimal(23.5)),

new Item("apple", 20, new BigDecimal(32.5)),

new Item("orange", 30, new BigDecimal(13.9)),

new Item("orange", 20, new BigDecimal(33.5)),

new Item("orange", 10, new BigDecimal(63.5)),

new Item("orange", 50, new BigDecimal(41.5)),

new Item("peach", 20, new BigDecimal(26.5)),

new Item("peach", 30, new BigDecimal(42.5)),

new Item("peach", 40, new BigDecimal(24.5)),

new Item("peach", 10, new BigDecimal(12.5))

);

// 分组,计数

Map counting = items.stream()

.collect(Collectors.groupingBy(Item::getName, Collectors.counting()));

System.out.println(counting);

// 分组,计数,数量

Map sum = items.stream()

.collect(Collectors.groupingBy(Item::getName, Collectors.summingInt(Item::getQty)));

System.out.println(sum);

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

4、通过价格分组

48304ba5e6f9fe08f3fa1abda7d326ab.png

public class Java8Example3 {

public static void main(String[] args) {

List items = Arrays.asList(

new Item("apple", 10, new BigDecimal(23.5)),

new Item("apple", 20, new BigDecimal(32.5)),

new Item("orange", 30, new BigDecimal(13.9)),

new Item("orange", 20, new BigDecimal(32.5)),

new Item("orange", 10, new BigDecimal(63.5)),

new Item("orange", 50, new BigDecimal(41.5)),

new Item("peach", 20, new BigDecimal(26.5)),

new Item("peach", 30, new BigDecimal(32.5)),

new Item("peach", 40, new BigDecimal(24.5)),

new Item("peach", 10, new BigDecimal(12.5))

);

// 分组

Map> groupByPriceMap = items.stream()

.collect(Collectors.groupingBy(Item::getPrice));

System.out.println(groupByPriceMap);

// 分组 转化List->Set

Map> result = items.stream()

.collect(Collectors.groupingBy(Item::getPrice, Collectors.mapping(Item::getName, Collectors.toSet())));

System.out.println(result);

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值