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

前端 未结 2 1973
忘了有多久
忘了有多久 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<Class<?>> stream = StreamEx.iterate(value.getClass(), Class::getSuperClass)
                                      .takeWhile(Objects::nonNull);
    

    In JDK-9 just replace StreamEx with Stream.

    0 讨论(0)
  • 2021-01-18 16:17

    Here's the same code as presented in the question but implemented using a Spliterator. I am hoping someone can come up with a simpler, smarter answer.

      Class<?> clazz;
      Object value;
    
      value = ...;
    
      <Class<?>>walkLinks(value.getClass(), Class::getSuperclass).
         ...
    
    
    public static <T> Stream<T> walkLinks(T start, Function<T, T> next)
    {
       WalkLinks<T> walker;
       Stream<T> result;
    
       walker = new WalkLinks<>(start, next);
       result = StreamSupport.stream(walker, false);
    
       return(result);
    }
    
    private static class WalkLinks<T> extends Spliterators.AbstractSpliterator<T>
    {
       private final Function<T, T> m_next;
       private       T              m_value;
    
       public WalkLinks(T value, Function<T, T> next)
       {
          super(Long.MAX_VALUE, Spliterator.ORDERED + Spliterator.NONNULL);
    
          m_value = value;
          m_next  = next;
       }
    
       @Override
       public boolean tryAdvance(Consumer<? super T> action)
       {
          if (m_value == null)
             return(false);
    
          action.accept(m_value);
    
          m_value = m_next.apply(m_value);
    
          return(true);
       }
    }
    
    0 讨论(0)
提交回复
热议问题