美文网首页CWIKIUS
Java 从一个 List 中删除 null 元素

Java 从一个 List 中删除 null 元素

作者: HoneyMoose | 来源:发表于2022-05-06 05:10 被阅读0次

概述

本文章主要是为了展示如何从一个 List 列表中删除所有的 null 元素。

在本文中,我们使用了下面的几个实现:

纯 Java

Guava

Apache Commons Collections

Java 8 提供的 lambda 表达式

使用纯 Java 来将 List 中的 null 元素删除

Java 中的 Collections 框架提供了一个简单的解决方案:

基于使用 while 循环将 List 列表中的所有空元素进行删除。

@TestpublicvoidgivenListContainsNulls_whenRemovingNullsWithPlainJava_thenCorrect(){finalList list = Lists.newArrayList(null,1,null);while(list.remove(null));                assertThat(list, hasSize(1));    }

可选的,我们可以使用一个更加简单的方法,使用 list 中使用 removeAll 的方法来将 null 删除。

@TestpublicvoidgivenListContainsNulls_whenRemovingNullsWithPlainJavaAlternative_thenCorrect(){finalList list = Lists.newArrayList(null,1,null);        list.removeAll(Collections.singleton(null));        assertThat(list, hasSize(1));    }

需要注意的是,上面 2 个方法将会对输入的 List 进行修改。

在删除后得到的 list 是修改后的list

使用 Guava

我们还可以使用 Guava 的方法来进行 null 的查询和处理,这个需要通过 Java 的 predicates。

@TestpublicvoidgivenListContainsNulls_whenRemovingNullsWithGuavaV1_thenCorrect(){finalList list = Lists.newArrayList(null,1,null);        Iterables.removeIf(list, Predicates.isNull());        assertThat(list, hasSize(1));    }

如上面使用的代码,首先需要对 List 进行遍历,Iterables。List 对 Guava 来说是 Iterables。

然后使用Predicates.isNull()来进行判读。

如果为 NULL 就删除。

如果你不希望对输入的 List 进行修改的话,你可以使用 Guava 提供的 Iterables.filter 方法来进行遍历和处理。

如下面的代码,只需要将 removeIf 换成 filter 就可以了。

@TestfinalListlist= Lists.newArrayList(null,1,null,2,3);finalList listWithoutNulls = Lists.newArrayList(Iterables.filter(list, Predicates.notNull()));        assertThat(listWithoutNulls, hasSize(3));}

使用 Apache Commons Collections

Apache Commons Collections 的使用和 Guava 的使用是类似的。

虽然 Apache Commons Collections 使用了 filter 方法,但是需要注意的,上面的方法会对输入的 List 进行修改。

@TestpublicvoidgivenListContainsNulls_whenRemovingNullsWithCommonsCollections_thenCorrect(){finalList list = Lists.newArrayList(null,1,2,null,3,null);        CollectionUtils.filter(list, PredicateUtils.notNullPredicate());        assertThat(list, hasSize(3));}

Java 8 Lambdas 表达式

Java 8 提供了 Lambdas 表达式来对 null 进行处理。

这个处理可以是并行的,也可以是序列进行的。

请查看下面的代码:

@TestpublicvoidgivenListContainsNulls_whenFilteringParallel_thenCorrect() {finalListlist= Lists.newArrayList(null,1,2,null,3,null);finalList listWithoutNulls =list.parallelStream().filter(Objects::nonNull).collect(Collectors.toList());        assertThat(listWithoutNulls, hasSize(3));    }    @TestpublicvoidgivenListContainsNulls_whenFilteringSerial_thenCorrect() {finalListlist= Lists.newArrayList(null,1,2,null,3,null);finalList listWithoutNulls =list.stream().filter(Objects::nonNull).collect(Collectors.toList());        assertThat(listWithoutNulls, hasSize(3));    }    @TestpublicvoidgivenListContainsNulls_whenRemovingNullsWithRemoveIf_thenCorrect() {finalList listWithoutNulls = Lists.newArrayList(null,1,2,null,3,null);        listWithoutNulls.removeIf(Objects::isNull);        assertThat(listWithoutNulls, hasSize(3));    }

使用上面的方法能够让你比较快速的将 List 中的 null 元素进行删除。

结论

在本文中,我们对 List 中的 Null 对象如何删除进行了一些探讨。

通过上面的一些方法能够让你在 Java 进行编程的时候快速删除 List 中的 null 元素。

https://www.ossez.com/t/java-list-null/13940

相关文章

网友评论

    本文标题:Java 从一个 List 中删除 null 元素

    本文链接:https://www.haomeiwen.com/subject/lbvuyrtx.html