JDK8对List进行分组操作(stream的groupby)

1. 数据准备:

public class TestGroupBy {
    @Data
    public static class User {
        private Integer id;
        private Integer schoolId;
        private String userName;
        private String edu;
        private double price;
    public static List<TestListToMap.User> users = new ArrayList<>();
    static {
        TestListToMap.User u1 = new TestListToMap.User();
        u1.setId(1001);
        u1.setSchoolId(100);
        u1.setUserName("小1");
        u1.setEdu("001");
        u1.setPrice(0.01);
        TestListToMap.User u2 = new TestListToMap.User();
        u2.setId(1002);
        u2.setSchoolId(100);
        u2.setUserName("小2");
        u2.setEdu("002");
        u2.setPrice(0.20);
        TestListToMap.User u3 = new TestListToMap.User();
        u3.setId(2010);
        u3.setSchoolId(200);
        u3.setUserName("小3");
        u3.setEdu("001");
        u3.setPrice(3.00);
        TestListToMap.User u4 = new TestListToMap.User();
        u4.setId(3001);
        u4.setSchoolId(300);
        u4.setEdu("001");
        u4.setPrice(40.0);
        users.add(u1);
        users.add(u2);
        users.add(u3);
        users.add(u4);

对List进行分组,也可以理解为将List转换为Map集合。

若想将返回的结果映射为不同的集合。

    public static void main(String[] args) {
        List<String> lists=new ArrayList<>();
        lists.add("a");
        lists.add("b");
        lists.add("a");
        lists.add("a");
        //将最终结果映射为LinkedHashSet结构。
        LinkedHashSet<String> collect = lists.stream().
                collect(Collectors.toCollection(LinkedHashSet::new));
        System.out.println(collect);

2. group by的重载方法

group by生成一个拥有分组功能的Collector,有三个重载方法。

  • 需要一个参数:按照该参数进行分组。结果返回一个Map集合,每个Map的key默认是分组参数的类型,value是一个List集合。
  • public void test1() {
        Map <String,List < User >> collect = users.stream().collect(Collectors.groupingBy(User: :getEdu));
    
  • 需要两个参数:第二参数是Collector类型,可以对value进行处理。
  • 2.1 可以对结果进行映射

    public void test2() {
        Map <String,List <Integer>> collect = users.stream().collect(Collectors.groupingBy(User: :getEdu,
        //第二个参数对Map的value进行处理(映射)
        Collectors.mapping(User: :getId, Collectors.toList())));
    

    2.2 可以对结果进行求和

    public static void test3() {
        Map <String,Double> collect = users.stream().collect(Collectors.groupingBy(User: :getEdu,
        //对参数进行累计求和
        Collectors.summingDouble(User: :getPrice)));
        System.out.println(collect);
    

    2.3 对结果的统计

    public static void test4() {
        Map < String,Long > collect = users.stream().collect(Collectors.groupingBy(User: :getEdu,
        //获取count数量
        Collectors.counting()));
        System.out.println(collect);
    
  • 需要三个参数,第三个参数添加了对结果Map的生成方式,默认是HashMap
  • public static void test3() {
        Map <String,Double > collect = users.stream().collect(Collectors.groupingBy(User: :getEdu,
        //决定map的生成方式,使用TreeMap
        TreeMap: :new,
        //对参数进行累计求和
        Collectors.summingDouble(User: :getPrice)));
        System.out.println(collect);