zoukankan      html  css  js  c++  java
  • Stream集合的一些常用操作记录(属性去重重新生成、等...)

    1.Collectors.collectingAndThen

    下面例子是:在UserEntity的集合中,根据Id属性去重,生成一个新的集合

    package com.test.lamdba;
    
    import com.test.entity.UserEntity;
    
    import java.util.ArrayList;
    import java.util.Comparator;
    import java.util.List;
    import java.util.TreeSet;
    import java.util.function.Function;
    import java.util.function.Supplier;
    import java.util.stream.Collectors;
    
    public class TestCollectors {
    
        /**
         * 在UserEntity的集合中,根据Id属性去重,生成一个新的集合
         *
         * @param args
         */
        public static void main(String[] args) {
            List<UserEntity> userEntityList = new ArrayList<>();
            userEntityList.add(new UserEntity("1", "zhangsan"));
            userEntityList.add(new UserEntity("2", "lisi"));
            userEntityList.add(new UserEntity("3", "wangwu"));
            userEntityList.add(new UserEntity("4", "zhaoliu"));
            userEntityList.add(new UserEntity("2", "sunqi"));
            userEntityList.add(new UserEntity("3", "qianba"));
    
    
            /**
             * 根据Id属性去重,生成一个新的集合
             */
            ArrayList<UserEntity> result = userEntityList.stream().collect(
                    Collectors.collectingAndThen(
                            Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(UserEntity::getId))),
                            ArrayList::new
                    )
            );
    
            result.forEach(System.out::println);
            System.out.println("---------------------------------------");
    
    
            /**
             * 上线方法看着过于复杂,把他改成匿名函数的形式写下,便于看懂
             */
            ArrayList result2 = userEntityList.stream().collect(Collectors.collectingAndThen(
                    Collectors.toCollection(new Supplier<TreeSet<UserEntity>>() {
                        @Override
                        public TreeSet<UserEntity> get() {
                            return new TreeSet<>(Comparator.comparing(UserEntity::getId));
                        }
                    }),
                    new Function<TreeSet<UserEntity>, ArrayList>() {
                        @Override
                        public ArrayList apply(TreeSet treeSet) {
                            return new ArrayList(treeSet);
                        }
                    }
            ));
    
            result2.forEach(System.out::println);
        }
    
    } 

    Console:

    UserEntity(id=1, name=zhangsan)
    UserEntity(id=2, name=lisi)
    UserEntity(id=3, name=wangwu)
    UserEntity(id=4, name=zhaoliu)
    ---------------------------------------
    UserEntity(id=1, name=zhangsan)
    UserEntity(id=2, name=lisi)
    UserEntity(id=3, name=wangwu)
    UserEntity(id=4, name=zhaoliu) 

    对于Collectors.collectingAndThen、toCollection的使用参考了以下博客:

    https://blog.csdn.net/qq_35634181/article/details/108867857

    --

  • 相关阅读:
    《DSP using MATLAB》Problem 8.9
    《DSP using MATLAB》Problem 8.8
    PID库与PID基本优化(四)
    PID库与PID基本优化(三)
    PID库与PID基本优化(二)
    PID库与PID基本优化(一)
    Mahony姿态解算算法笔记(二)
    《理解矩阵》笔记
    Mahony姿态解算算法笔记(一)
    洛谷p1082 同余方程
  • 原文地址:https://www.cnblogs.com/tenWood/p/15264672.html
Copyright © 2011-2022 走看看