5 Ways to Remove Null from ArrayList

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

[java]

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());

[/java]

 

Way 2: Using removeAll with one null value list

[java]

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());

[/java]

 

Way 3: Using Iterator

[java]

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());

[/java]

 

Way 4: Using while loop

[java]

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

[/java]

 

Way 5: Using removeIf

[java]

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

[/java]

 

 

Consolidated Java Program:

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

 

[java]

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());

}
}

[/java]

 

Console Output:

[plain]

5

2

[/plain]

 

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.

 

Leave a Reply