Remove Multiple Null Values From A List in Java

I’ve had situations where I’ve needed a list of foreign keys (fks) that I get from a result set and from that list make additional queries. Sometimes for whatever reason there are null values in the list and I have to remove them. You might had a similar problem where you needed to remove multiple occurring value from a lists in java. There are a few ways you can approach this problem. You you can remove each occurrence of a list element one at a time. The example below removes any null element in the list one at a time.

while(ids.contains(null)) {

There is another approach you can use to remove all instances of a given value from a list in one go. Instead of the remove method you can use the removeAll as in the following code sample.


Of course, instead of null values you can remove all instances of any given value from a list based on your business needs.

One Response to “Remove Multiple Null Values From A List in Java”

Leave a Reply