java delete all null values in list
This article introduces how to delete all null values in the list, which are implemented in the normal Java way, Guava, Apache Commons Collections and java8 lambda expression way.
Normal java mode
The java Collection framework provides a simple solution through the basic while loop:
@Test
public void givenListContainsNulls_whenRemovingNullsWithPlainJava_thenCorrect() {
List<Integer> list = Lists.newArrayList(null, 1, null);
while (list.remove(null));
assertThat(list, hasSize(1));
}
remove method when the parameter is null, the for loop is also behind it. Deleting null returns true. Continue the outer loop until all nulls are deleted. Of course, we can also use the removeAll method:
@Test
public void givenListContainsNulls_whenRemovingNullsWithPlainJavaAlternative_thenCorrect() {
List<Integer> list = Lists.newArrayList(null, 1, null);
list.removeAll(Collections.singleton(null));
assertThat(list, hasSize(1));
}
It should be noted that both methods affect the original list.
Guava mode
You can also use Google Guava to provide a more practical way to use predicte:
@Test
public void givenListContainsNulls_whenRemovingNullsWithGuavaV1_thenCorrect() {
List<Integer> list = Lists.newArrayList(null, 1, null);
Iterables.removeIf(list, Predicates.isNull());
assertThat(list, hasSize(1));
}
In addition, if we don't want to modify the original list, we can create a new list:
@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));
}
Apache Commons Collections mode
Apache Commons Collections provides a simple way to implement, using a similar functional programming method:
@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));
}
In this way, the original list is also modified.
Java 8 Lambdas mode
Finally, let's look at the java 8 lambda expression solution to filter the list, and the filtering process can be parallel or serial:
@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));
}
Well, the way java8 provides is flexible and easy to use.
summary
We show different ways to delete null values in the list, a quick and useful solution. I hope you like it.