Stream流對List集合排序、分組、過濾、收集組裝、聚合處理等

代碼和注釋如下:

        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());


        //聚合函數(shù)(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
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容