移除 Java 列表中的所有空值

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

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

Java 集合框架提供了一个简单的解决方案来移除 List 中的所有空元素------一个基本的 while 循环:

hljs-copy-wrapper 复制代码
@Test
public void givenListContainsNulls_whenRemovingNullsWithPlainJava_thenCorrect() {
    List<Integer> list = Lists.newArrayList(null, 1, null);
    while (list.remove(null));

    assertThat(list, hasSize(1));
}

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

hljs-copy-wrapper 复制代码
@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 和更函数式的做法,通过谓词来移除空值:

hljs-copy-wrapper 复制代码
@Test
public void givenListContainsNulls_whenRemovingNullsWithGuavaV1_thenCorrect() {
    List<Integer> list = Lists.newArrayList(null, 1, null);
    Iterables.removeIf(list, Predicates.isNull());

    assertThat(list, hasSize(1));
}

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

hljs-copy-wrapper 复制代码
@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 库的简单解决方案,采用类似的函数式风格:

hljs-copy-wrapper 复制代码
@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 解决方案;这个过滤过程可以并行进行或串行进行:

hljs-copy-wrapper 复制代码
@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 中移除空值的不同方法。

相关推荐
渣哥3 小时前
有一天,我和 CopyOnWriteArrayList 杯“线程安全”的咖啡
java
叽哥3 小时前
Kotlin学习第 3 课:Kotlin 流程控制:掌握逻辑分支与循环的艺术
android·java·kotlin
杨杨杨大侠3 小时前
第5章:实现Spring Boot集成
java·github·eventbus
华仔啊3 小时前
工作5年没碰过分布式锁,是我太菜还是公司太稳?网友:太真实了!
java·后端
尚久龙3 小时前
安卓学习 之 图片控件和图片按钮
android·java·学习·手机·android studio·安卓
摸鱼仙人~3 小时前
深入理解 MyBatis-Plus 的 `BaseMapper`
java·开发语言·mybatis
杨杨杨大侠3 小时前
第6章:高级特性与性能优化
java·github·eventbus
Dcs3 小时前
代码评审还能更好!
java
刃神太酷啦4 小时前
C++ 异常处理机制:从基础到实践的全面解析----《Hello C++ Wrold!》(20)--(C/C++)
java·c语言·开发语言·c++·qt·算法·leetcode
蓝倾9764 小时前
小红书获取用户作品列表API接口操作指南
java·服务器·前端·python·电商开放平台·开放api接口