How to create an infinite Stream out of an Iterator?

前端 未结 4 1686
甜味超标
甜味超标 2020-12-09 03:14

Looking at the following class I\'ve made:

public class FibonacciSupplier implements Iterator {
    private final IntPredicate hasNextPredicat         


        
4条回答
  •  甜味超标
    2020-12-09 03:59

    You can use the low level stream support primitives and the Spliterators library to make a stream out of an Iterator.

    The last parameter to StreamSupport.stream() says that the stream is not parallel. Be sure to let it like that because your Fibonacci iterator depends on previous iterations.

    return StreamSupport.stream( Spliterators.spliteratorUnknownSize( new Iterator()
    {
        @Override
        public boolean hasNext()
        {
            // to implement
            return ...;
        }
    
        @Override
        public ContentVersion next()
        {
            // to implement
            return ...;
        }
    }, Spliterator.ORDERED ), false );
    

提交回复
热议问题