• 集合根据对象时间属性按年月分组


     public static void main(String[] args) {
            List<Student> list = new ArrayList<>();
            list.add(new Student("A",10,LocalDateTime.now()));
            list.add(new Student("B",11,LocalDateTime.now().minus(2, ChronoUnit.MONTHS)));
            list.add(new Student("C",12,LocalDateTime.now().minus(2, ChronoUnit.MONTHS)));
            list.add(new Student("D",13,LocalDateTime.now().minus(3, ChronoUnit.MONTHS)));
    
            Map<String, List<Student>> map = list.stream().collect(Collectors.groupingBy(t -> t.getDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM"))));
          
        }
        static class Student{
            private String name;
            private Integer age;
            private LocalDateTime dateTime;
    
            public Student(String name, Integer age, LocalDateTime dateTime) {
                this.name = name;
                this.age = age;
                this.dateTime = dateTime;
            }
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public Integer getAge() {
                return age;
            }
    
            public void setAge(Integer age) {
                this.age = age;
            }
    
            public LocalDateTime getDateTime() {
                return dateTime;
            }
    
            public void setDateTime(LocalDateTime dateTime) {
                this.dateTime = dateTime;
            }
        }
  • 相关阅读:
    设计规范理解
    JVM读书笔记
    springboot整合RabbitMQ
    springboot 整合Redis
    tomcat原理
    配置嵌入式Servlet容器
    Springboot自动配置原理
    Springboot启动原理
    Springboot配置文件加载顺序
    修改VisualSVN Server地址为ip地址,修改svn服务端地址为ip或者域名地址的方法
  • 原文地址:https://www.cnblogs.com/panbingqi/p/14150646.html
Copyright © 2020-2023  润新知