5 Ways to Remove Null from ArrayList

(Last Updated On: March 23, 2017)

5 Ways to Remove Null from ArrayList: Removing null from arraylist become some realtime business scenario many times. Removing null from arraylist is actually easy as it can be. We have many ways to remove null from arraylist.

 

5 Ways to Remove Null From ArrayList:

  1. Using collections.singleton
  2. Using removeAll with one null value list
  3. Using Iterator
  4. Using while loop (rarely used)
  5. Using removeIf (rarely used)

 

Below program is shared with all the above 5 ways to remove null from arraylist. By understanding all these five ways you can get better understanding about the arraylist collection and it’s uses.

 

If you are around 5+ years experience then you will be expected to solve these things at your core java interviews.

 

5 Ways to Remove Null From ArrayList Program – Java Source Code :

 

Way 1: Using collections.singleton


System.out.println("Before Removing Null From List >>> " + testLst.size());
testLst.removeAll(Collections.singleton(null));
System.out.println("After Removing Null From List >>> " + testLst.size());

 

Way 2: Using removeAll with one null value list


List<String> nullLst = new ArrayList<String>();
nullLst.add(null);
System.out.println("Before Removing Null From List >>> " + testLst.size());
testLst.removeAll(nullLst);
System.out.println("After Removing Null From List >>> " + testLst.size());

 

Way 3: Using Iterator


System.out.println("Before Removing Null From List >>> " + testLst.size());
Iterator<String> itr = testLst.iterator();
while (itr.hasNext()) {
if (itr.next() == null) {
itr.remove();
}
}
System.out.println("After Removing Null From List >>> " + testLst.size());

 

Way 4: Using while loop


System.out.println("Before Removing Null From List >>> " + testLst.size());
while (testLst.remove(null));
System.out.println("After Removing Null From List >>> " + testLst.size());

 

Way 5: Using removeIf


System.out.println("Before Removing Null From List >>> " + testLst.size());
testLst.removeIf(Objects::isNull);
System.out.println("After Removing Null From List >>> " + testLst.size());

 

 

Consolidated Java Program:

When you want to run/test any 1 way, then comment all the other four ways.

 


package in.javadomain;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

public class RemoveNullEmptyFromList {

public static void main(String[] args) {
List<String> testLst = new ArrayList<String>();
testLst.add("monday");
testLst.add(null);
testLst.add(null);
testLst.add(null);
testLst.add("sunday");
// Way 1 -> Directly using collections.singleton
System.out.println("Before Removing Null From List >>> " + testLst.size());
testLst.removeAll(Collections.singleton(null));
System.out.println("After Removing Null From List >>> " + testLst.size());

// Way 2 -> With One more list & removeAll
List<String> nullLst = new ArrayList<String>();
nullLst.add(null);
System.out.println("Before Removing Null From List >>> " + testLst.size());
testLst.removeAll(nullLst);
System.out.println("After Removing Null From List >>> " + testLst.size());

// Way 3 - Using Iterator
System.out.println("Before Removing Null From List >>> " + testLst.size());
Iterator<String> itr = testLst.iterator();
while (itr.hasNext()) {
if (itr.next() == null) {
itr.remove();
}
}
System.out.println("After Removing Null From List >>> " + testLst.size());

// Way 4 - Using while loop
System.out.println("Before Removing Null From List >>> " + testLst.size());
while (testLst.remove(null));
System.out.println("After Removing Null From List >>> " + testLst.size());

// Way 5 - using removeIf
System.out.println("Before Removing Null From List >>> " + testLst.size());
testLst.removeIf(Objects::isNull);
System.out.println("After Removing Null From List >>> " + testLst.size());

}
}

 

Console Output:


5

2

 

Note: All the 5 ways prints the same output as above. Ensure you are commenting all the other 4 ways than the one you would like to test/run.

 

235 total views, 1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *