Reputation: 1658
There is some Java code:
List<Call> updatedList = updatingUniquedList
.stream()
.map(s -> {
Call call = callsBufferMap.get(s);
}
return call;
}).collect(Collectors.toList());
How to avoid avoid to add to final list if call variable is null ?
Upvotes: 9
Views: 15892
Reputation: 1460
Maybe you can do something like this:
Collectors.filtering(Objects::nonNull, Collectors.toList())
Upvotes: 2
Reputation: 4607
There are several options you can use:
.filter(Objects::nonNull)
updatedList.removeIf(Objects::isNull);
So for example the lines can look like this:
List<Call> updatedList = updatingUniquedList
.stream()
.map(callsBufferMap::get)
.filter(Objects::nonNull)
.collect(Collectors.toList());
Upvotes: 4
Reputation: 49656
.filter(Objects::nonNull)
before collecting. Or rewrite it to a simple foreach with an if.
Btw, you can do
.map(callsBufferMap::get)
Upvotes: 14
Reputation: 2981
You can use .filter(o -> o != null)
after map
and before collect
.
Upvotes: 4