Home > Back-end >  How to filter in stream?
How to filter in stream?

Time:02-08

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;
}

CodePudding user response:

Add a filter step:

openingHoursDto.setBranches(items.stream()
        .filter (e -> e.getSummary() != null)
        .collect(Collectors.groupingBy(Event::getSummary))
        ...

CodePudding user response:

Or you can use .stream() .filter(Objects::notNull)

CodePudding user response:

Something like this would work:

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;
}

CodePudding user response:

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

  •  Tags:  
  • Related