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);
}
}
0 Comments