Java 8 Stream of Super Classes, Parent Files, Component Parents, linked list, etc

前端 未结 2 1974
忘了有多久
忘了有多久 2021-01-18 15:29

I would like to convert the following for statement to a Java 8 stream (i.e. Stream>). The ideal solution would be simple eno

2条回答
  •  爱一瞬间的悲伤
    2021-01-18 16:10

    You need a takeWhile method which will appear in JDK-9 only. Currently you can use a back-port which is available, for example, in my StreamEx library:

    Stream> stream = StreamEx.iterate(value.getClass(), Class::getSuperClass)
                                      .takeWhile(Objects::nonNull);
    

    In JDK-9 just replace StreamEx with Stream.

提交回复
热议问题