Remove null elements from list
In java 8 you can use Collection.removeIf
:
list.removeIf(Objects::isNull);
This should work:
list.removeAll(Collections.singleton(null));
Extend ArrayList
and override add()
& addAll()
method and simply don't allow null
or you could use list.removeAll(null);
as shown here Which internally iterates the loop