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 : [[]]

相关推荐
everyStudy22 分钟前
JS中判断字符串中是否包含指定字符
开发语言·前端·javascript
luthane24 分钟前
python 实现average mean平均数算法
开发语言·python·算法
码农研究僧29 分钟前
Flask 实现用户登录功能的完整示例:前端与后端整合(附Demo)
python·flask·用户登录
Ylucius31 分钟前
动态语言? 静态语言? ------区别何在?java,js,c,c++,python分给是静态or动态语言?
java·c语言·javascript·c++·python·学习
凡人的AI工具箱42 分钟前
AI教你学Python 第11天 : 局部变量与全局变量
开发语言·人工智能·后端·python
sleP4o1 小时前
Python操作MySQL
开发语言·python·mysql
是店小二呀1 小时前
【C++】C++ STL探索:Priority Queue与仿函数的深入解析
开发语言·c++·后端
洛寒瑜1 小时前
【读书笔记-《30天自制操作系统》-23】Day24
开发语言·汇编·笔记·操作系统·应用程序
ephemerals__1 小时前
【c++】动态内存管理
开发语言·c++
咩咩觉主1 小时前
en造数据结构与算法C# 群组行为优化 和 头鸟控制
开发语言·c#