When working with Java lists, it is not uncommon to encounter null values. Null values can cause issues when performing operations on the list, and it is often necessary to remove them to ensure the integrity of the data. In this blog post, we will explore different approaches to remove null values from Java list.
Table of Contents
Using an Iterator
One way to remove null values from a Java list is by using an iterator. Iterators provide a way to traverse through the elements of a list and perform operations on them.
Following is an example:
List<String> list = new ArrayList<>();
list.add("Apple");
list.add(null);
list.add("Banana");
list.add(null);
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
if (iterator.next() == null) {
iterator.remove();
}
}
System.out.println(list);
In the above code snippet, we create a list with some null values. We then create an iterator and iterate through the list. If we encounter a null value, we remove it using the iterator’s remove()
method.
Output:
[Apple, Banana]
Using Java 8 Stream
Java 8 introduced the Stream API, which provides a powerful way to perform operations on collections. We can leverage the Stream API to remove null values from a Java list.
Following is an example:
List<String> list = new ArrayList<>();
list.add("Apple");
list.add(null);
list.add("Banana");
list.add(null);
list = list.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
System.out.println(list);
In the above code snippet, we create a list with some null values. We then convert the list to a stream using the stream()
method. We apply a filter using the Objects::nonNull
method reference to remove the null values. Finally, we collect the filtered stream back into a list using the collect()
method.
Output:
[Apple, Banana]
Using Apache Commons Collections
If you are using the Apache Commons Collections library, you can make use of the CollectionUtils
class to remove null values from a Java list.
Following is an example:
List<String> list = new ArrayList<>();
list.add("Apple");
list.add(null);
list.add("Banana");
list.add(null);
CollectionUtils.filter(list, Objects::nonNull);
System.out.println(list);
In the above code snippet, we create a list with some null values. We then use the filter()
method from the CollectionUtils
class to remove the null values from the list.
Output:
[Apple, Banana]
Using Java 8 removeIf
Another approach to remove null values from a Java list is by using the removeIf()
method introduced in Java 8.
Following is an example:
List<String> list = new ArrayList<>();
list.add("Apple");
list.add(null);
list.add("Banana");
list.add(null);
list.removeIf(Objects::isNull);
System.out.println(list);
In the above code snippet, we create a list with some null values. We then use the removeIf()
method along with the Objects::isNull
method reference to remove the null values from the list.
Output:
[Apple, Banana]
Conclusion
Null values in a Java list can cause issues when working with the data. It is important to remove null values to ensure the integrity of the list. In this blog post, we explored different approaches to remove null values from a Java list, including using an iterator, Java 8 Stream API, Apache Commons Collections, and the Java 8 removeIf()
method. Choose the approach that best suits your requirements and coding style.