Check instanceof in stream

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

I have the following expression:

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


        
相关标签:
3条回答
  • 2021-01-30 10:27

    There is a small problem with @Eran solution - typing class name in both filter and map is error-prone - it is easy to forget to change the name of the class in both places. An improved solution would be something like this:

    private static <T, R> Function<T, Stream<R>> select(Class<R> clazz) {
        return e -> clazz.isInstance(e) ? Stream.of(clazz.cast(e)) : null;
    }
    
    scheduleIntervalContainers
      .stream()
      .flatMap(select(ScheduleIntervalContainer.class))
      .filter( sic -> sic.getStartTime() != sic.getEndTime())
      .collect(Collectors.toList());   
    

    However there might be a performance penalty in creating a Stream for every matching element. Be careful to use it on huge data sets. I've learned this solution from @Tagir Vailev

    0 讨论(0)
  • 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() );
    
    0 讨论(0)
  • 2021-01-30 10:51

    You can apply another filter in order to keep only the ScheduleIntervalContainer instances, and adding a map will save you the later casts :

    scheduleIntervalContainers.stream()
        .filter(sc -> sc instanceof ScheduleIntervalContainer)
        .map (sc -> (ScheduleIntervalContainer) sc)
        .filter(sic -> sic.getStartTime() != sic.getEndTime())
        .collect(Collectors.toList());
    

    Or, as Holger commented, you can replace the lambda expressions with method references if you prefer that style:

    scheduleIntervalContainers.stream()
        .filter(ScheduleIntervalContainer.class::isInstance)
        .map (ScheduleIntervalContainer.class::cast)
        .filter(sic -> sic.getStartTime() != sic.getEndTime())
        .collect(Collectors.toList());
    
    0 讨论(0)
提交回复
热议问题