Skip to main content

How to remove all nulls from a Collection in Java

How to remove nulls from a List in Java


  • One way is to iterate the objects from the Collection ( like List, Array, Map etc) and remove null objects.
  • Remove a particular object from List, Array or Map by searching the object and then removing it.
  • Using the Collections.singleton(T o) method from the java.util package and then it will remove all the objects from the collection.

Removing Objects in Traditional Way.

We will be taking the following list of String for all our Examples

String init[] = { "One", "Two", "One", "Three", "One", "Two", "Three","Four", "Two" };

Here we will remove the objects by finding it from the Collection.

List list = new ArrayList(Arrays.asList(init));
list.remove("One");
System.out.println("List value: "+list);
list.remove("Two");
System.out.println("List value: "+list);
list.remove("Three");
System.out.println("List value: "+list);
Output of the above program will be as follows.
List value: [Two, One, Three, One, Two, Three, Four, Two]
List value: [One, Three, One, Two, Three, Four, Two]
List value: [One, One, Two, Three, Four, Two]

Removing objects from Collection using Collections.singleton() method

String init[] = { "One", "Two", "One", "Three", "One", "Two", "Three","Four", "Two" };

Here we will remove the objects by finding it from the Collection.

List list = new ArrayList(Arrays.asList(init));
list.removeAll(Collections.singleton("One"));
System.out.println("List value: "+list);
list.removeAll(Collections.singleton("Two"));
System.out.println("List value: "+list);
list.removeAll(Collections.singleton("Three"));
System.out.println("List value: "+list);
Output of the above program will be as follows.
List value: [Two, Three, Two, Three, Four, Two]
List value: [Three, Three, Four]
List value: [Four]

Removing nulls from the Collection List

We will be using the following list for the following programs.

String init[] = {  "One", "Two", null, "One", "Three",null, "One", "Two",null, "Three","Four", "Two", null }; 
import java.util.*;
public class Demo {
public static void main(String args[]) {
// create an array of string objs
String init[] = {
"One", "Two", null, "One", "Three", null, "One", "Two", null, "Three", "Four", "Two", null
};
// create two lists
List < String > list = new ArrayList(Arrays.asList(init));
List < String > listNew = new ArrayList < String > ();
for (String str: list) {
if (str != null) listNew.add(str);
}
System.out.println("List value: " + listNew);
}
}

Output of the above program will be as follows.

List value: [One, Two, One, Three, One, Two, Three, Four, Two]

The above code may not very efficient as it creates two list objects.

Remove all null values using Collectiosn.singleton()

import java.util.*;
public class Demo {
public static void main(String args[]) {
// create an array of string objs
String init[] = {
"One", "Two", null, "One", "Three", null, "One", "Two", null, "Three", "Four", "Two", null
};
// create two lists
List < String > list = new ArrayList(Arrays.asList(init));
list.removeAll(Collections.singleton(null));
System.out.println("List value: " + list);
}
}

Output of the above program will be as follows.

List value: [One, Two, One, Three, One, Two, Three, Four, Two]

Comments