zoukankan      html  css  js  c++  java
  • 《Java8实战》05

    笔记简记

    1. Collectors.groupingBy 简单使用
    2. Collectors.toMap 简单使用

    数据准备 POJO测试类

    @Data
    class InnerClass {
        private Long id;
        private String name;
        private LocalDateTime birthDay;
        public InnerClass() {
        }
        InnerClass(Long id, String name, LocalDateTime desc) {
            this.id = id;
            this.name = name;
            this.birthDay = desc;
        }
    }
    

    实现<Id, POJO>的一对一Map代码及实现

     InnerClass clazz1 = new InnerClass(1L, "name1", LocalDateTime.now());
            InnerClass clazz2 = new InnerClass(2L, "name2", LocalDateTime.now().minusYears(1));
            InnerClass clazz3 = new InnerClass(3L, "name3", LocalDateTime.now().minusYears(2));
    
            ArrayList<InnerClass> allList = new ArrayList<>();
            allList.add(clazz1);
            allList.add(clazz2);
            allList.add(clazz3);
    
            // (Collectors.groupingBy() 的最简单的默认接口实现
            //     public static <T, K> Collector<T, ?, Map<K, List<T>>>
            //    groupingBy(Function<? super T, ? extends K> classifier) {
            //        return groupingBy(classifier, toList());
            //    }
    
            Map<Long, List<InnerClass>> collect = allList.stream().collect(Collectors.groupingBy(InnerClass::getId));
            collect.forEach((k, v) -> {
                System.out.println("<Id, 对实体>  " + k + "  " + v);
            });
            // 生成<Id, 对实体>的Map 打印如下::::::::::::::::::::::::
            //1  [InnerClass{id=1, name='name1', birthDay=2020-06-12T16:17:54.150}]
            //2  [InnerClass{id=2, name='name2', birthDay=2019-06-12T16:17:54.151}]
            //3  [InnerClass{id=3, name='name3', birthDay=2018-06-12T16:17:54.151}]
    

    <Id, 某个属性> 一对一的KV键值对

           /**
             * 创建<Id, name> 一对一的KV键值对list
             */
    
            Map<Long, String> longStringMap = allList.stream().collect(Collectors.toMap(InnerClass::getId, InnerClass::getName));
            longStringMap.forEach((k, v) -> {
                System.out.println("<Id, name>	" + k + "  " + v);
            });
            //<Id, name>	1  name1
            //<Id, name>	2  name2
            //<Id, name>	3  name3
    

    创建<Id, List > 一对多的KV键值对list

            /**
             * 创建<Id, List<name> > 一对多的KV键值对list
             */
            InnerClass clazz22 = new InnerClass(2L, "name222", LocalDateTime.now().minusYears(1));
            InnerClass clazz33 = new InnerClass(3L, "name333", LocalDateTime.now().minusYears(2));
            allList.add(clazz22);
            allList.add(clazz33);
    
            Map<Long, List<InnerClass>> collect1 = allList.stream().collect(Collectors.groupingBy(InnerClass::getId));
            collect1.forEach((k, v) -> {
                System.out.println("<Id, List<实体> >  " + k + "  " + v);
            });
             // 打印如下::::::::::::::::::::::::
            //<Id, List<实体> >  1  [InnerClass{id=1, name='name1', birthDay=2020-06-12T16:48:29.381}]
            //<Id, List<实体> >  2  [InnerClass{id=2, name='name2', birthDay=2019-06-12T16:48:29.382}, InnerClass{id=2, name='name222', birthDay=2019-06-12T16:48:29.404}]
            //<Id, List<实体> >  3  [InnerClass{id=3, name='name3', birthDay=2018-06-12T16:48:29.382}, InnerClass{id=3, name='name333', birthDay=2018-06-12T16:48:29.404}]
    
  • 相关阅读:
    获取华为OID
    win10 mongodb的安装
    第一次使用plotly画图遇到的问题
    Java Serialable序列化
    yield(),sleep(),join()
    线程的启动的两种方法,Runnable接口,run()的调用
    Thread的中断机制
    Oracle VM VirtualBox 修改备份位置
    潜在因子算法
    Linux--常用命令
  • 原文地址:https://www.cnblogs.com/zhazhaacmer/p/13100577.html
Copyright © 2011-2022 走看看