Java8语法糖

/ 默认分类 / 没有评论 / 22浏览

两个集合合并到一块并去重



    public static void main(String[] args) {


        Ren ren1 = new Ren();
        ren1.setAge(1);
        ren1.setName("测试1");
        Ren ren2 = new Ren();
        ren2.setAge(2);
        ren2.setName("测试2");
        List<Ren>  list1 =  Arrays.asList(ren1,ren2);

        Ren ren3 = new Ren();
        ren3.setAge(3);
        ren3.setName("测试3");
        Ren ren4 = new Ren();
        ren4.setAge(4);
        ren4.setName("测试4");
//        List<Ren>  list2 =  new ArrayList<>();
        List<Ren>  list2 =  Arrays.asList(ren3,ren4);
        List<Ren> renList = Stream.of(list1, list1).flatMap(Collection::stream).distinct().collect(Collectors.toList());
        renList.forEach(System.out::println);





    }
    static class  Ren{
        private String name;
        private Integer age;

        @Override
        public String toString() {
            return "Ren{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }

        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  List<CurriculumRespDTO> getSortList1(List<CurriculumRespDTO> list) {
        Collections.sort(list,(o1, o2)-> (o1.getScheduleDay() + o1.getStartTime() ).compareTo(o2.getScheduleDay() + o2.getStartTime())    );
        return list;
    }