当前位置: 首页 > news >正文

移除 Java 列表中的所有空值

这个快速教程将展示如何使用纯 Java、Guava、Apache Commons Collections 以及较新的 Java 8 Lambda 支持来移除 List 中的所有空元素。


1. 使用纯 Java 从列表中移除空值


Java 集合框架提供了一个简单的解决方案来移除 List 中的所有空元素——一个基本的 while 循环:
@Test
public void givenListContainsNulls_whenRemovingNullsWithPlainJava_thenCorrect() {List<Integer> list = Lists.newArrayList(null, 1, null);while (list.remove(null));assertThat(list, hasSize(1));
}


此外,我们还可以使用以下简单方法:

@Test
public void givenListContainsNulls_whenRemovingNullsWithPlainJavaAlternative_thenCorrect() {List<Integer> list = Lists.newArrayList(null, 1, null);list.removeAll(Collections.singleton(null));assertThat(list, hasSize(1));
}


请注意,这两种解决方案都会修改源列表。

2. 使用 Google Guava 从列表中移除空值

我们也可以使用 Guava 和更函数式的做法,通过谓词来移除空值:

@Test
public void givenListContainsNulls_whenRemovingNullsWithGuavaV1_thenCorrect() {List<Integer> list = Lists.newArrayList(null, 1, null);Iterables.removeIf(list, Predicates.isNull());assertThat(list, hasSize(1));
}


如果不想修改源列表,Guava 将允许我们创建一个新的过滤列表:

@Test
public void givenListContainsNulls_whenRemovingNullsWithGuavaV2_thenCorrect() {List<Integer> list = Lists.newArrayList(null, 1, null, 2, 3);List<Integer> listWithoutNulls = Lists.newArrayList(Iterables.filter(list, Predicates.notNull()));assertThat(listWithoutNulls, hasSize(3));
}

3. 使用 Apache Commons Collections 从列表中移除空值

现在我们来看一个使用 Apache Commons Collections 库的简单解决方案,采用类似的函数式风格:

@Test
public void givenListContainsNulls_whenRemovingNullsWithCommonsCollections_thenCorrect() {List<Integer> list = Lists.newArrayList(null, 1, 2, null, 3, null);CollectionUtils.filter(list, PredicateUtils.notNullPredicate());assertThat(list, hasSize(3));
}

请注意,此解决方案也会修改原始列表。

4. 使用 Lambda 表达式从列表中移除空值(Java 8)


最后——让我们来看一个使用 Lambda 表达式过滤 List 的 Java 8 解决方案;这个过滤过程可以并行进行或串行进行:

@Test
public void givenListContainsNulls_whenFilteringParallel_thenCorrect() {List<Integer> list = Lists.newArrayList(null, 1, 2, null, 3, null);List<Integer> listWithoutNulls = list.parallelStream().filter(Objects::nonNull).collect(Collectors.toList());
}@Test
public void givenListContainsNulls_whenFilteringSerial_thenCorrect() {List<Integer> list = Lists.newArrayList(null, 1, 2, null, 3, null);List<Integer> listWithoutNulls = list.stream().filter(Objects::nonNull).collect(Collectors.toList());
}public void givenListContainsNulls_whenRemovingNullsWithRemoveIf_thenCorrect() {List<Integer> listWithoutNulls = Lists.newArrayList(null, 1, 2, null, 3, null);listWithoutNulls.removeIf(Objects::isNull);assertThat(listWithoutNulls, hasSize(3));
}

就是这样——一些快速且非常实用的解决方案,用于清除 List 中的所有空值元素。

5. 结论


在本文中,我们探索了使用 Java、Guava 或 Lambda 表达式从 List 中移除空值的不同方法。
http://www.dtcms.com/a/265899.html

相关文章:

  • 一天两道力扣(1)
  • Linux多线程(十二)之【生产者消费者模型】
  • “Payload document size is larger than maximum of 16793600.“问题解决(MongoDB)
  • Kettle数据抽取(十一)作业-邮件
  • 什么是码率?剪映中如何选择适合的视频码率
  • C++(std::sort)
  • js-cookie详细介绍
  • Node.js与Webpack
  • 2025年6月:技术探索与生活平衡的协奏曲
  • 目标检测:从基础原理到前沿技术全面解析
  • 架构师的“降维打击”:用桥接模式,把 N*M 的问题变成 N+M
  • Matplotlib 安装使用教程
  • 【Git】同时在本地使用多个github账号进行github仓库管理
  • C++ 网络编程(14) asio多线程模型IOThreadPool
  • 【数据结构】树的基本操作
  • 阿里云服务网格ASM实践
  • 抗辐照芯片在核电厂火灾探测器中的应用优势与性能解析
  • springMvc的简单使用:要求在浏览器发起请求,由springMVC接受请求并响应,将个人简历信息展示到浏览器
  • Java 原生 HTTP Client
  • https如何利用工具ssl证书;使用自己生成的证书
  • http、SSL、TLS、https、证书
  • 【交互设计】UI 与 UX 简介:从核心概念到行业实践
  • 微算法科技(NASDAQ MLGO)基于量子图像处理的边缘检测算法:开拓图像分析新视野
  • [2025CVPR]SEEN-DA:基于语义熵引导的领域感知注意力机制
  • 通过观看数百个外科手术视频讲座来学习多模态表征|文献速递-最新论文分享
  • 【数据结构】哈希——闭散列/开散列模拟实现(C++)
  • [论文阅读] 人工智能 | 在非CUDA硬件上运行几何学习:基于Intel Gaudi-v2 HPU的PyTorch框架移植实践
  • Stable Diffusion 项目实战落地:AI照片修复 第一篇 从黑白到彩色:用AI给照片上色的魔法之旅
  • stm32f103c8t6---ymodem协议串口IAP升级(只教怎么操作,略讲原理,100%成功!)
  • laravel基础:隐式模型绑定的用法和介绍