jdk1.8:stream流式分组groupby

package com.example.apidemo.jdk18;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang3.tuple.MutablePair;

/**
 * @Author Tim
 * @Date 2021/12/17 16:24
 */
public class GroupBy {

    public static void main(String[] args) {

        //Java 8允许在接口中加入具体方法。接口中的具体方法有两种,default方法和static方法,identity()就是Function接口的一个静态方法。
        //Function.identity()返回一个输出跟输入一样的Lambda表达式对象,等价于形如t -> t形式的Lambda表达式

        //按输入类型,统计分组数量
        List<String> items = Arrays.asList("apple", "apple", "banana","apple", "orange", "banana", "papaya");
        Map<String, Long> result = items.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        System.out.println("result===" +result);

        //按输入类型,统计分组--排序
        Map<String, List<String>> result2 = items.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.toList()));
        System.out.println("result2===" +result2);

        // result 排序
        Map<String, Long> finalMap = new LinkedHashMap<>();
        result.entrySet().stream().sorted(Map.Entry.<String, Long>comparingByValue().reversed())
            .forEachOrdered(e -> finalMap.put(e.getKey(), e.getValue()));
        System.out.println("finalMap===" + finalMap);


        List<User> users = Arrays.asList(
            new User("apple", 10, new BigDecimal(23)),
            new User("apple", 20, new BigDecimal(32)),

            new User("orange", 30, new BigDecimal(13)),
            new User("orange", 20, new BigDecimal(33)),
            new User("orange", 10, new BigDecimal(63)),

            new User("peach", 20, new BigDecimal(26)),
            new User("peach", 30, new BigDecimal(42)),
            new User("peach", 40, new BigDecimal(24)),
            new User("peach", 10, new BigDecimal(12))
        );

        // 按照name分组,并求出个数
        Map<String, Long> counting = users.stream().collect(Collectors.groupingBy(User::getName, Collectors.counting()));
        System.out.println("counting===="+counting);

        // 按照name分组,求和
        Map<String, Integer> sum = users.stream().collect(Collectors.groupingBy(User::getName, Collectors.summingInt(User::getQty)));
        System.out.println("sum===="+sum);

        int basePoint3 = users.stream().filter(user -> user.getName().equals("orange")).mapToInt(User::getQty).sum();
        System.out.println("basePoint3===="+ basePoint3);

        System.out.println("=================================================================");

        // 按照name分组 转化List->Set,并且只取 getName作为新的list。
        Map<Integer, List<String>> result_list = users.stream()
            .collect(Collectors.groupingBy(User::getQty, Collectors.mapping(User::getName, Collectors.toList())));
        System.out.println("result_list===="+ result_list);

        Map<String, Set<User>> result_set = users.stream()
            .collect(Collectors.groupingBy(User::getName, Collectors.toSet()));
        System.out.println("result_set===="+ result_set);

        //按照name分组,可以简写,不需要,Collectors.toList()
        Map<String, List<User>> groupByPriceMap = users.stream().collect(Collectors.groupingBy(User::getName));
        System.out.println("groupByPriceMap===="+ groupByPriceMap);
    }

}

 

package com.example.apidemo.jdk18;

import java.math.BigDecimal;

public class User {

    private String name;

    private int qty;

    private BigDecimal price;

    public User() {
    }

    public User(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 +
                '}';
    }
}

 

posted @ 2021-12-21 10:43  威兰达  阅读(5587)  评论(0编辑  收藏  举报