Java program to remove nulls from a List Container
Last Updated :
11 Dec, 2018
List is an ordered collection of objects which allows to store duplicate values or
null values, in the insertion order. So it is very important to remove null values in many scenarios.
Examples:
Input: [Geeks, null, forGeeks, null, A computer portal]
Output: [Geeks, forGeeks, A computer portal]
Input: [1, null, 2, 3, null, 4]
Output: [1, 2, 3, 4]
Below are the methods to remove nulls from a List in Java:
- Using List.remove() List interface provides a pre-defined method remove(element) which is used to remove a single occurrence of the element passed, from the List, if found.
Algorithm:
- Get the list with null values.
- Repeatedly call remove(null) on the list, until all null values are removed.
- Return/Print the list (now with all null values removed).
Java
// Java Program to remove nulls
// from a List using List.remove()
import java.util.*;
class GFG {
public static void main(String[] args)
{
// Create the list with null values
List<String> list = new ArrayList<>(
Arrays.asList("Geeks",
null,
"forGeeks",
null,
"A computer portal"));
// Print the list
System.out.println("Initial List: " + list);
// Removing nulls using List.remove()
// Repeatedly call remove() till all null are removed
while (list.remove(null)) {
}
// Print the list
System.out.println("Modified List: " + list);
}
}
Output:
Initial List: [Geeks, null, forGeeks, null, A computer portal]
Modified List: [Geeks, forGeeks, A computer portal]
- Using List.removeAll(): List interface provides another pre-defined method removeAll(Collection) which is used to remove all occurrences of the elements of the Collection passed, from the List, if found.
Algorithm:
- Get the list with null values.
- Create a Collection with only null as element using Collections.singletonList(null)
- Call removeAll(Collection) on the list once.
- Return/Print the list (now with all null values removed).
Java
// Java Program to remove nulls
// from a List using List.removeAll()
import java.util.*;
class GFG {
public static void main(String[] args)
{
// Create the list with null values
List<String> list = new ArrayList<>(
Arrays.asList("Geeks",
null,
"forGeeks",
null,
"A computer portal"));
// Print the list
System.out.println("Initial List: " + list);
// Removing nulls using List.removeAll()
// passing a collection with single element "null"
list.removeAll(Collections.singletonList(null));
// Print the list
System.out.println("Modified List: " + list);
}
}
Output:
Initial List: [Geeks, null, forGeeks, null, A computer portal]
Modified List: [Geeks, forGeeks, A computer portal]
- Using iterator: Iterator is an interface which belongs to collection framework. It allows user to traverse the collection, access the data element and remove the data elements of the collection.
Algorithm:
- Get the list with null values.
- Create an iterator from the list
- Traverse through each element of the List with the of created Iterator
- Check, for each element, if it is null. If found null, call IteratorElement.remove() on that element.
- Return/Print the list (now with all null values removed).
Program:
Java
// Java Program to remove nulls
// from a List using iterator
import java.util.*;
class GFG {
// Generic function to remove Null Using Iterator
public static <T> List<T> removeNullUsingIterator(List<T> list)
{
// Create an iterator from the list
Iterator<T> itr = list.iterator();
// Find and remove all null
while (itr.hasNext()) {
if (itr.next() == null)
itr.remove(); // remove nulls
}
// Return the null
return list;
}
public static void main(String[] args)
{
// Create the list with null values
List<String> list = new ArrayList<>(
Arrays.asList("Geeks",
null,
"forGeeks",
null,
"A computer portal"));
// Print the list
System.out.println("Initial List: " + list);
// Removing nulls using iterator
list = removeNullUsingIterator(list);
// Print the list
System.out.println("Modified List: " + list);
}
}
Output:
Initial List: [Geeks, null, forGeeks, null, A computer portal]
Modified List: [Geeks, forGeeks, A computer portal]
- Using Guava Iterables removeIf(): Guava Iterables class provides Iterables.removeIf(Iterable, Predicate) that removes every element from a specified Iterable (or Collections that implements Iterable) that satisfies the provided predicate.
Algorithm:
- Get the list with null values.
- Get the Predicate condition Predicates.isNull() to pass in the argument of removeIf()
- Call Iterables.removeIf(List, Predicate) where List is the original list with null values and the Predicate is the Predicates.isNull() instance.
- Return/Print the list (now with all null values removed).
Java
// Java Program to remove nulls
// from a List using Guava Iterables
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// Create the list with null values
List<String> list = new ArrayList<>(
Arrays.asList("Geeks",
null,
"forGeeks",
null,
"A computer portal"));
// Print the list
System.out.println("Initial List: " + list);
// Removing nulls using Guava Iterables
// using Predicate condition isNull()
Iterables.removeIf(list, Predicates.isNull());
// Print the list
System.out.println("Modified List: " + list);
}
}
Output:
Initial List: [Geeks, null, forGeeks, null, A computer portal]
Modified List: [Geeks, forGeeks, A computer portal]
- Using Apache Commons Collections filter(): Apache Commons Collections CollectionUtils class provides filter(Iterable, Predicate) that removes every element from a specified iterable that do not satisfies the provided predicate.
Algorithm:
- Get the list with null values.
- Get the Predicate condition PredicateUtils.notNullPredicate() to pass in the argument of filter() such that the elements passing the condition of NotNull remain in the list, while all other get filtered.
- Call CollectionUtils.filter(list, PredicateUtils.notNullPredicate()) where List is the original list with null values and the Predicate is the PredicateUtils.notNullPredicate() instance.
- Return/Print the list (now with all null values removed).
Program:
Java
// Java Program to remove nulls
// from a List using Apache Common COllection Filter()
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.PredicateUtils;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// Create the list with null values
List<String> list = new ArrayList<>(
Arrays.asList("Geeks",
null,
"forGeeks",
null,
"A computer portal"));
// Print the list
System.out.println("Initial List: " + list);
// Removing nulls using Apache Common filter()
// using Predicate condition notNullPredicate()
CollectionUtils.filter(list, PredicateUtils.notNullPredicate());
// Print the list
System.out.println("Modified List: " + list);
}
}
Output:
Initial List: [Geeks, null, forGeeks, null, A computer portal]
Modified List: [Geeks, forGeeks, A computer portal]
- Using Lambdas (Java 8): Stream.filter() method can be used in Java 8 that returns a stream consisting of the elements
that match the given predicate condition.
Algorithm:
- Get the list with null values.
- Create a Stream from the list using list.stream()
- Filter the stream of elements that are not null using list.filter(x -> x != null)
- Collect back the Stream as List using .collect(Collectors.toList()
- Return/Print the list (now with all null values removed).
Java
// Java Program to remove nulls
// from a List using Apache Common COllection Filter()
import java.util.stream.Collectors;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// Create the list with null values
List<String> list = new ArrayList<>(
Arrays.asList("Geeks",
null,
"forGeeks",
null,
"A computer portal"));
// Print the list
System.out.println("Initial List: " + list);
// Removing nulls using Java Stream
// using Predicate condition in lambda expression
list = list.stream()
.filter(x -> x != null)
.collect(Collectors.toList());
// Print the list
System.out.println("Modified List: " + list);
}
}
Output:
Initial List: [Geeks, null, forGeeks, null, A computer portal]
Modified List: [Geeks, forGeeks, A computer portal]
Similar Reads
How to remove a SubList from a List in Java Given a list in Java, the task is to remove all the elements in the sublist whose index is between fromIndex, inclusive, and toIndex, exclusive. The range of the index is defined by the user. Example: Input list = [1, 2, 3, 4, 5, 6, 7, 8], fromIndex = 2, endIndex = 4 Output [1, 2, 5, 6, 7, 8] Input
2 min read
Convert List to Set in Java Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element. Few important features of the Java Set interface are as follows: The set interface is an unordered collection of objec
5 min read
Remove elements from a List that satisfy given predicate in Java Below are the methods to efficiently remove elements from a List satisfying a Predicate condition: p ==> Predicate, specifying the condition l ==> List, from which element to be removed Using iterator Below program demonstrates the removal of null elements from the list, using the Predicate Ja
5 min read
List remove(int index) method in Java with Examples The remove(int index) method of List interface in Java is used to remove an element from the specified index from a List container and returns the element after removing it. It also shifts the elements after the removed element by 1 position to the left in the List. Example:Java// Java Program Demon
2 min read
Remove all elements from the ArrayList in Java Prerequisite: ArrayList in Java Given an ArrayList, the task is to remove all elements of the ArrayList in Java. Examples: Input: ArrayList = [1, 2, 3, 4] Output: ArrayList = [] Input: ArrayList = [12, 23, 34, 45, 57, 67, 89] Output: ArrayList = [] Using clear() method: Syntax: collection_name.clear
2 min read
How to remove an element from ArrayList in Java? ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package. With
4 min read