Java中Stream流对List集合排序、分组、过滤、收集组装、聚合处理等

代码和注释如下:


        List testDtoList = new ArrayList<>();
        testDtoList.add(new TestDto("张三","北京",20));
        testDtoList.add(new TestDto("李四","北京",35));
        testDtoList.add(new TestDto("王五","北京",31));
        testDtoList.add(new TestDto("赵六","上海",34));
        testDtoList.add(new TestDto("孙七","上海",18));


        //按年龄排序
        List reverseResList = testDtoList.stream().sorted(Comparator.comparing(TestDto::getAge).reversed()).collect(Collectors.toList());
        List optionalList = Optional.ofNullable(testDtoList).orElse(null).stream().sorted(Comparator.comparing(TestDto::getAge).reversed()).collect(Collectors.toList());



        //按单属性分组
        Map> groupResList = testDtoList.stream().collect(Collectors.groupingBy(TestDto::getAddress));
        //按多属性分组
        Map&

你可能感兴趣的:(Java基础,Lambda和Stream流,java)