📜  java 迭代器流式传输 - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:26.056000             🧑  作者: Mango

代码示例1
// Java program to get a Stream
// from a given Iterator
  
import java.util.*;
import java.util.stream.*;
  
class GFG {
  
    // Function to get the Stream
    public static  Stream
    getStreamFromIterator(Iterator iterator)
    {
  
        // Convert the iterator to Spliterator
        Spliterator
            spliterator = Spliterators
                              .spliteratorUnknownSize(iterator, 0);
  
        // Get a Sequential Stream from spliterator
        return StreamSupport.stream(spliterator, false);
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Get the Iterator
        Iterator
            iterator = Arrays.asList(1, 2, 3, 4, 5)
                           .iterator();
  
        // Get the Stream from the Iterator
        Stream
            stream = getStreamFromIterator(iterator);
  
        // Print the elements of stream
        stream.forEach(s -> System.out.println(s));
    }
}