'How to filter null returning method in stream?
I have a list, it has some method like getSummary(), and this method returns null in some cases. How can I filter to avoid getting the null result?
Here is my code:
List<Event> items = getEvents(sharedCalendarId, startDateTime, endDateTime, zoneId);
if (CollectionUtils.isNotEmpty(items)) {
openingHoursDto.setBranches(items.stream()
.collect(Collectors.groupingBy(Event::getSummary))
.entrySet()
.stream()
.map(e -> {
LibraryBranchOpeningHoursDto lBOHDto = new LibraryBranchOpeningHoursDto();
lBOHDto.setName(e.getKey());
lBOHDto.setDates(getHoursDtoMap(e.getValue(), startDateTime, endDateTime, selfServicePattern, specialHoursColorId, zoneId));
return lBOHDto;
})
.sorted(comparing(LibraryBranchOpeningHoursDto::getName))
.collect(Collectors.toList()));
return openingHoursDto;
}
Solution 1:[1]
Add a filter step:
openingHoursDto.setBranches(items.stream()
.filter (e -> e.getSummary() != null)
.collect(Collectors.groupingBy(Event::getSummary))
...
Solution 2:[2]
Or you can use .stream() .filter(Objects::notNull)
Solution 3:[3]
first you don't need to validate if List<Event> items is empty or not, stream will not be processed if there are no elements..
openingHoursDto.setBranches(items.stream()
//filter to not only null but also empty string
// or use .filter(Objects::nonNull) for the whole object to be filtered
.filter(event -> !event.getSummary().isEmpty())
.collect(Collectors.groupingBy(Event::getSummary))
.entrySet()
.stream()
..map(this::toLBOHDto)
.sorted(comparing(LibraryBranchOpeningHoursDto::getName))
.collect(Collectors.toList()));
return openingHoursDto;
private LibraryBranchOpeningHoursDto toLBOHDto(Event e){
LibraryBranchOpeningHoursDto lBOHDto = new LibraryBranchOpeningHoursDto();
lBOHDto.setName(e.getKey());
lBOHDto.setDates(getHoursDtoMap(e.getValue(), startDateTime, endDateTime, selfServicePattern, specialHoursColorId, zoneId));
return lBOHDto;
}
it is better practice to have your mapping outside the stream operation like making it a method, or you can have it as you have it
Solution 4:[4]
A filter predicate, like the following, needs to be added in the stream:
.filter(event -> event.getSummary() != null)
The code after the addition is provided hereafter for reference:
List<Event> items = getEvents(sharedCalendarId, startDateTime, endDateTime, zoneId);
if (CollectionUtils.isNotEmpty(items)) {
openingHoursDto.setBranches(items.stream()
//filter to keep only events with non-null event.getSummary()
.filter(event -> event.getSummary() != null)
.collect(Collectors.groupingBy(Event::getSummary))
.entrySet()
.stream()
.map(e -> {
LibraryBranchOpeningHoursDto lBOHDto = new LibraryBranchOpeningHoursDto();
lBOHDto.setName(e.getKey());
lBOHDto.setDates(getHoursDtoMap(e.getValue(), startDateTime, endDateTime, selfServicePattern, specialHoursColorId, zoneId));
return lBOHDto;
})
.sorted(comparing(LibraryBranchOpeningHoursDto::getName))
.collect(Collectors.toList()));
return openingHoursDto;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mark Rotteveel |
| Solution 2 | antey13 |
| Solution 3 | Mohammed Fataka |
| Solution 4 |
