ListUtils中筛选数据方式select

ListUtils中筛选数据方式select

用法说明

java 复制代码
    /**
     * Selects all elements from input collection which match the given
     * predicate into an output list.
     * 从给出的集合中筛选符合对应断言的数据,最终汇总一个新的集合。
     * <p>
     * A <code>null</code> predicate matches no elements.
     *
     * @param <E> the element type
     * @param inputCollection  the collection to get the input from, may not be null 集合不为空
     * @param predicate  the predicate to use, may be null 
     * @return the elements matching the predicate (new list)
     * @throws NullPointerException if the input list is null 如果集合为空,空指针异常
     *
     * @since 4.0
     * @see CollectionUtils#select(Iterable, Predicate)
     */
    public static <E> List<E> select(final Collection<? extends E> inputCollection,
            final Predicate<? super E> predicate) {
        return CollectionUtils.select(inputCollection, predicate, new ArrayList<E>(inputCollection.size()));
    }

实践用法

造数据方法

java 复制代码
    public List<User> buildData() {
        List<User> result = Lists.newArrayList();
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                result.add(User.builder().userName("小" + i).age(i * 2).loginTime(DateUtil.parse("2023-08-09", com.alibaba.excel.util.DateUtils.DATE_FORMAT_10))
                        .account(new BigDecimal("1"))
                        .build());
            } else {
                result.add(User.builder().userName("小" + i).age(i * 3).loginTime(DateUtil.parse("2023-08-10", com.alibaba.excel.util.DateUtils.DATE_FORMAT_10))
                        .account(new BigDecimal("2"))
                        .build());
            }
        }
        return result;
    }

集合为空处理

java 复制代码
   List<User> buildData = null;
   if (CollectionUtils.isNotEmpty(buildData)){
       List<User> result = ListUtils.select(buildData
               , item -> item.getAccount().compareTo(new BigDecimal("1")) == 0);
       log.info("result : [{}]", result);
   }

集合不为空处理

java 复制代码
 List<User> buildData = buildData();
 if (CollectionUtils.isNotEmpty(buildData)){
        List<User> result = ListUtils.select(buildData
                , item -> item.getAccount().compareTo(new BigDecimal("1")) == 0);
        log.info("result : [{}]", result);
    }

09:15:04.509 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - result : [[NoDaoTest.User(userName=小0, age=0, loginTime=Wed Aug 09 00:00:00 CST 2023, account=1), NoDaoTest.User(userName=小2, age=4, loginTime=Wed Aug 09 00:00:00 CST 2023, account=1), NoDaoTest.User(userName=小4, age=8, loginTime=Wed Aug 09 00:00:00 CST 2023, account=1), NoDaoTest.User(userName=小6, age=12, loginTime=Wed Aug 09 00:00:00 CST 2023, account=1), NoDaoTest.User(userName=小8, age=16, loginTime=Wed Aug 09 00:00:00 CST 2023, account=1)]]
断言为空时,返回空集合

java 复制代码
List<User> buildData = buildData();
        if (CollectionUtils.isNotEmpty(buildData)){
            List<User> result = ListUtils.select(buildData
                    , null);
            log.info("result : [{}]", result);
        }

09:13:47.765 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - result : [[]]

相关推荐
oioihoii11 分钟前
C++11 尾随返回类型:从入门到精通
java·开发语言·c++
Wyn_21 分钟前
【QT】qtdesigner中将控件提升为自定义控件后,css设置样式不生效(已解决,图文详情)
开发语言·qt
MYH51627 分钟前
汽车停车匹配充电桩随机森林
python·随机森林·汽车
伍六星28 分钟前
更新Java的环境变量后VScode/cursor里面还是之前的环境变量
java·开发语言·vscode
Dola_Pan31 分钟前
Android四大组件通讯指南:Kotlin版组件茶话会
android·开发语言·kotlin
鼓掌MVP36 分钟前
Python多线程编程:从GIL锁到实战优化
python
天天爱吃肉821842 分钟前
【十年技术演进深度解构:车载充电机(OBC)将成为新能源汽车的“能源大脑”】
python·嵌入式硬件·算法·汽车·能源
这里有鱼汤42 分钟前
有人说10日低点买入法,赢率高达95%?我不信,于是亲自回测了下…
后端·python
万能程序员-传康Kk43 分钟前
智能教育个性化学习平台-java
java·开发语言·学习
道剑剑非道1 小时前
QT开发技术【ffmpeg + QAudioOutput】音乐播放器
开发语言·qt·ffmpeg