Header Ads Widget

Remove Null Values from ArrayList in Java 8 or Java Higher Versions

Below are some of the easiest methods to remove the null values from the array list in java. There are 2 methods listed below, first one is we can remove the null values using the Collection removeIf and the second one using the Java 8 Streams.

1. Using Collections [Method - Collection.removeIf()]

The removeIf method in ArrayList is used to remove the set of values from an ArrayList with a predicate condition. removeIf method will accept a predicate condition and according to the predicate, the ArrayList will remove the values. Below we are accepting the Objects::isNull condition and according to the condition we are removing all the null values from the method. In the example first System.out.println will print the initial array with null values and the second time it will remove the null values and print the result in the console.

JavaCollection.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

public class JavaCollection {

    public static void main(String args[]) {
        ArrayList<String> fruits = new ArrayList<>(
                Arrays.asList("Apple", "Orange", null, "Pineapple", "Kiwi", null));
        String valuesBeforeNullRemoval = fruits.stream().collect(Collectors.joining(","));

        // The Below method Collection.removeIf() method will remove the null
        // values from the fruits list.
        fruits.removeIf(Objects::isNull);

        String valuesAfterNullRemoval = fruits.stream().collect(Collectors.joining(","));

        // This below code will print the non null fruits details in the
        // console.
        System.out.print("Fruits value before null removal:- ");
        System.out.print(valuesBeforeNullRemoval);

        // This below code will print the non null fruits details in the
        // console.
        System.out.print("\nFruits after null removal:- ");
        System.out.print(valuesAfterNullRemoval);

    }

}

Console Output

Fruits value before null removal:- Apple,Orange,null,Pineapple,Kiwi,null Fruits after null removal:- Apple,Orange,Pineapple,Kiwi


2. Using Java 8 Streams

We can filter any kind of data from the ArrayList using the Stream.filter() method. The filter() method will accept a predicate condition and if the predicate condition satisfies only it will go to the next step. In the below example the filter method will filter out the null values and only the non null values will be collected to a list and update in the fruits list.

JavaCollection.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class JavaCollection {

    public static void main(String args[]) {
        List<String> fruits = Arrays.asList("Apple", "Orange", null, "Pineapple", "Kiwi", null);
        String valuesBeforeNullRemoval = fruits.stream().collect(Collectors.joining(","));
       
        // filter(Objects::nonNull) method will filter out the null strings and pass the non null strings to collect method
        fruits = fruits.stream().filter(Objects::nonNull).collect(Collectors.toList());

        String valuesAfterNullRemoval = fruits.stream().collect(Collectors.joining(","));

        // This below code will print the non null fruits details in the
        // console.
        System.out.print("Fruits value before null removal:- ");
        System.out.print(valuesBeforeNullRemoval);

        // This below code will print the non null fruits details in the
        // console.
        System.out.print("\nFruits after null removal:- ");
        System.out.print(valuesAfterNullRemoval);

    }

}


Console Output

Fruits value before null removal:- Apple,Orange,null,Pineapple,Kiwi,null Fruits after null removal:- Apple,Orange,Pineapple,Kiwi

Remove null values from arraylist in java8



These are the two methods that could be used to remove the null values from the ArrayList. If the above code is not working, please let us know in the comment section.

Post a Comment

0 Comments