gpt4 book ai didi

java - 如何过滤ArrayList中的空值

转载 作者:行者123 更新时间:2023-11-29 08:23:38 25 4
gpt4 key购买 nike

我有一个非常大的代码。我遇到了一个问题,在代码的最开始,null 被插入到 arraylist cuSTList 中,即 cuSTList 看起来像 [null]。在多行代码之后,我在使用 cuSTList 构建 Predicate 的地方更改了代码,如下所示:

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Customer> q = cb.createQuery(Customer.class);

Root<Customer> root = q.from(Customer.class);
q.select(root);
if (!CollectionUtils.isEmpty(custlist)){
Predicate predicate1 =root.get("custid").in(custlist);
}

q.where(predicate1);
TypedQuery<Customer> tq = em.createQuery(q);
List<Customer> allcust= tq.getResultList();

因此自 (cuSTList != null && !cuSTList.isEmpty()) 以来第一次检查通过了并建立了谓词。这在执行最终查询时给了我错误。我的问题是 - 无论如何我可以确保谓词仅在 cuSTList 没有空值时创建。我的问题是 - 就像 Collectionutils.isEmpty() 同时检查 null 和空性一样,如果有人可以帮助我提供一些 API 也可以检查 null inside arraylist

最佳答案

这是一个通用的解决方案:

private static ArrayList<String> list = new ArrayList<String>();
public static void main(String[] args) {
list.add("foo");
list.add("bar");
if (!list.isEmpty() && !list.contains(null))
System.out.println("ok");
list.add(null);
if (!list.isEmpty() && !list.contains(null))
System.out.println("not ok");
}

结果

ok

关于java - 如何过滤ArrayList中的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55348565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com