How to remove null reference in a Java List

If you have a list like below

List list = new ArrayList();

list.add("item1");
list.add("item2");
list.add(null);
list.add("item3");
list.add(null);
list.add("item4");
list.add("null");

Following one liner will remove all the null enteries from list

list.removeAll(Collections.singleton(null));

One Response
  • JavaBeginner Reply

    Thanks!

Leave a Reply