This quick tutorial is going to show how to remove all null elements from a List, using plain Java, Guava, the Apache Commons Collections and the newer Java 8 lambda support.
这个快速教程将展示如何使用普通的Java、Guava、Apache Commons Collections和较新的Java 8 lambda支持,从List中删除所有null元素,。
This article is part of the “Java – Back to Basic” series here on Baeldung.
本文是“Java – Back to Basic“系列的一部分,在Baeldung这里。
1. Remove Nulls From a List Using Plain Java
1.使用普通Java从List中删除空号
The Java Collections Framework offers a simple solution for removing all null elements in the List – a basic while loop:
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));
}
Alternatively, we can also use the following simple approach:
另外,我们也可以使用以下简单的方法。
@Test
public void givenListContainsNulls_whenRemovingNullsWithPlainJavaAlternative_thenCorrect() {
List<Integer> list = Lists.newArrayList(null, 1, null);
list.removeAll(Collections.singleton(null));
assertThat(list, hasSize(1));
}
Note that both these solutions will modify the source list.
请注意,这两种解决方案都会修改源列表。
2. Remove Nulls From a List Using Google Guava
2.使用Google Guava从List中删除空号
We can also remove nulls using Guava and a more functional approach, via predicates:
我们还可以使用Guava和一个更实用的方法,通过谓词来删除空值。
@Test
public void givenListContainsNulls_whenRemovingNullsWithGuavaV1_thenCorrect() {
List<Integer> list = Lists.newArrayList(null, 1, null);
Iterables.removeIf(list, Predicates.isNull());
assertThat(list, hasSize(1));
}
Alternatively, if we don’t want to modify the source list, Guava will allow us to create a new, filter list:
另外,如果我们不想修改源列表,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. Remove Nulls From a List Using Apache Commons Collections
3.使用Apache Commons集合从List中删除空号
Let’s now look at a simple solution using the Apache Commons Collections library using a similar functional style:
现在让我们来看看使用Apache Commons集合库的一个简单解决方案,使用类似的功能风格。
@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));
}
Note that this solution will also modify the original list.
请注意,这个解决方案也会修改原始列表。
4. Remove Nulls From a List Using Lambdas (Java 8)
4.使用Lambdas从List中移除Nulls(Java 8)
Finally – let’s look at a Java 8 solution using Lambdas to filter the List; the filtering process can be done in parallel or serial:
最后–让我们看看使用Lambdas过滤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));
}
And that’s it – some quick and very useful solutions for getting rid of all null elements from a List.
就是这样–一些快速且非常有用的解决方案,可以从List中去除所有的空元素。
5. Conclusion
5.总结
In this article, we were able to explore the different approaches we can have to remove nulls from a List using Java, Guava or Lambdas.
在这篇文章中,我们能够探索不同的方法,我们可以使用Java、Guava或Lambdas从List中移除空值。
The implementation of all of these examples and snippets can be found in the GitHub project. This is a Maven-based project so it should be easy to import and run.
所有这些例子和片段的实现都可以在GitHub项目中找到。这是一个基于Maven的项目,所以它应该很容易导入和运行。