Check instanceof in stream

前端 未结 3 1870
孤城傲影
孤城傲影 2021-01-30 09:51

I have the following expression:

scheduleIntervalContainers.stream()
        .filter(sic -> ((ScheduleIntervalContainer) sic).getStartTime() != ((ScheduleInte         


        
3条回答
  •  轮回少年
    2021-01-30 10:29

    A pretty elegant option is to use method reference of class:

    scheduleIntervalContainers
      .stream()
      .filter( ScheduleIntervalContainer.class::isInstance )
      .map( ScheduleIntervalContainer.class::cast )
      .filter( sic -> sic.getStartTime() != sic.getEndTime())
      .collect(Collectors.toList() );
    

提交回复
热议问题