Here some Easy way to remove null, blank values from ArrayList. 

Remove null from Arraylist
Remove null from Arraylist
 Most of the interview ask you how you can remove null or Blank value from ArrayList so we come
up with the best solution for you here is N number of way which you can use to remove null or blank value from ArrayList its depend on you which is more preferable for you business logic.

 Here is an easy way of removing all the null values from the or ArrayList of the collection in JAVA.

import java.util.ArrayList;
import java.util.List;

public class ArraylistRemoveNull {
 public static void main(String[] args) {

  List<Object> al = new ArrayList<Object>();
  al.add("A");
  al.add("B");
  al.add(73.9);
  al.add(null);
  al.add("D");
  al.add(100);
  al.add(12.66);
  al.add("E");
  al.add(null);
  al.add(455);
  al.add("F");
  al.add(null);
  al.add(233);
  al.add(30);
  al.add(null);
  al.add("G");
  al.add(34.55);
  al.add(null);
 }
}



The singleton() method returns an immutable set that contains only the specified object.
An application of this method is to remove an element from Collections like List and Set.
al.removeAll(Collections.singletonList(null));
System.out.println(al);

Objects.isNull is intended for use within Java 8 lambda filtering and was introduced for null safety.
al.removeIf(Objects::isNull);
System.out.println(al);

Removes all of this collection's elements that contain null value.
while(al.remove(null));
System.out.println(al);

You have to pass a collection containing null as a parameter to removeAll() method
List removenull=new ArrayList();
removenull.add(null);
al.removeAll(removenull); 

Post a Comment

Previous Post Next Post