Is there any easy way we could replace a value in a List or Collection if the value is null?
We can always do list.stream().filter(Objects::nonNu
Try this.
public static void replaceIf(List list, Predicate predicate, T replacement) {
for (int i = 0; i < list.size(); ++i)
if (predicate.test(list.get(i)))
list.set(i, replacement);
}
and
List list = Arrays.asList("a", "b", "c");
replaceIf(list, x -> x.equals("b"), "B");
System.out.println(list);
// -> [a, B, c]