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

2021-09-14  本文已影响0人  拄杖忙学轻声码

代码和注释如下:

        List<TestDto> 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<TestDto> reverseResList = testDtoList.stream().sorted(Comparator.comparing(TestDto::getAge).reversed()).collect(Collectors.toList());
        List<TestDto> optionalList = Optional.ofNullable(testDtoList).orElse(null).stream().sorted(Comparator.comparing(TestDto::getAge).reversed()).collect(Collectors.toList());


        //按地址分组
        Map<String, List<TestDto>> groupResList = testDtoList.stream().collect(Collectors.groupingBy(TestDto::getAddress));


        //按姓名过滤
        //单条件
        List<TestDto> oneCondition = testDtoList.stream().filter(testDto -> testDto.getName().equals("张三")).collect(Collectors.toList());
        //多条件
        List<TestDto> moreCondition = testDtoList.stream().filter(new Predicate<TestDto>() {
            @Override
            public boolean test(TestDto testDto) {
                if ("张三".equals(testDto.getName()) || "李四".equals(testDto.getName()) || testDto.getAge() > 30) {
                    return true;
                }
                return false;
            }
        }).collect(Collectors.toList());


        //收集年龄值组装成新的集合
        List<Integer> ageList = testDtoList.stream().map(testDto -> testDto.getAge()).collect(Collectors.toList());
        List<Integer> anotherList = testDtoList.stream().map(new Function<TestDto, Integer>() {
            @Override
            public Integer apply(TestDto testDto) {
                return testDto.getAge();
            }
        }).collect(Collectors.toList());


        //聚合函数(max、min、sum、count)
        int maxAge = testDtoList.stream().mapToInt(TestDto::getAge).max().getAsInt();
        int minAge = testDtoList.stream().mapToInt(TestDto::getAge).min().getAsInt();
        int sumAge = testDtoList.stream().mapToInt(TestDto::getAge).sum();
        long countAge = testDtoList.stream().map(TestDto::getAge).count();


        //组装
        TestDto reqTest1 = new TestDto();
        reqTest1.setStatusName("a");
        TestDto reqTest2 = new TestDto();
        reqTest2.setStatusName("b");
        TestDto reqTest3 = new TestDto();
        reqTest3.setStatusName("c");

        List<TestDto> testDtos = new ArrayList<>();
        testDtos.add(reqTest1);
        testDtos.add(reqTest2);
        testDtos.add(reqTest3);

        //组装成新的集合列表
        List<String> statusNameList = Optional.ofNullable(testDtos).orElse(new ArrayList<>()).stream().map(TestDto::getStatusName).collect(Collectors.toList());
        System.out.println("新的集合列表:" + statusNameList);
        //输出:新的集合列表:[a, b, c]


        //按照原来的字段值组装成新的字符串,以英文逗号分隔
        String delimiterStr = Optional.ofNullable(testDtos).orElse(new ArrayList<>()).stream().map(TestDto::getStatusName).collect(Collectors.joining(","));
        System.out.println("原来的字段值以逗号分隔组装:" + delimiterStr);
        //输出:原来的字段值以逗号分隔组装:a,b,c


        //在原来的字段值基础上处理追加其他字符串,以分号分隔
        String joiningStr = Arrays.stream(delimiterStr.split(",")).map(i -> {
            return i + 1;
        }).collect(Collectors.joining(";"));
        System.out.println("原来的字段值基础上处理追加其他字符串:" + joiningStr);
        //输出:原来的字段值基础上处理追加其他字符串:a1;b1;c1
上一篇下一篇

猜你喜欢

热点阅读