📜  从Java中的迭代器创建顺序流

📅  最后修改于: 2022-05-13 01:55:20.580000             🧑  作者: Mango

从Java中的迭代器创建顺序流

Java中的迭代器在 Collection Framework 中用于一一检索元素。

Java中的是来自数组或集合数据源的对象管道。顺序流是其中对象在同一处理系统上的单个流中流水线化的流。其他类型的流包括并行流,其中对象在多处理系统上流水线化。

因此,通常需要将迭代器用作顺序流。有很多方法可以做到,这些是:

  1. 使用 Spliterator : Spliterator 像其他迭代器一样,用于遍历源的元素。源可以是 Collection、IO 通道或生成器函数。

    使用的方法:

    ClassModifier and TypeMethodDescription
    Spliteratorsstatic SpliteratorspliteratorUnknownSize(Iterator iterator, int characteristics)Creates a Spliterator using a given Iterator as the source of elements, with no initial size estimate.
    StreamSupportstatic Streamstream(Spliterator spliterator, boolean parallel)Creates a new sequential or parallel Stream from a Spliterator.

    解释:Spliterator 在从 Iterator 创建 Sequential Stream 时充当中间体。 Iterator 首先在 Spliterators.spliteratorUnknownSize() 的帮助下转换为 Spliterator。在下面找到这个的方法描述。 Spliterator 然后在StreamSupport.stream()函数的帮助下转换为顺序流。该函数的第二个参数采用布尔值来确定要生成的流是否为并行。

    程序:

    // Java program to create a Sequential Stream
    // from an Iterator
      
    import java.util.*;
    import java.util.stream.*;
      
    class GfG {
        // Function to create a sequential Stream
        // from an Iterator
        public static  Stream 
                    iteratorToSequentialStream(Iterator itr)
        {
            // convert the iterator into a Spliterator
            Spliterator spitr = Spliterators.spliteratorUnknownSize(
                                               itr, Spliterator.NONNULL);
      
            // Convert spliterator into a sequential stream
            // The second parameter "false" passess whether 
            // the stream is to be created parallel or not
            return StreamSupport.stream(spitr, false);
        }
      
        public static void main(String[] args)
        {
            Iterator iterator = Arrays.asList("G", "E", "E", 
                                                   "K", "S").iterator();
      
            Stream stream = iteratorToSequentialStream(iterator);
              
            System.out.println("Sequential Stream : " + 
                        stream.collect(Collectors.toList()));
        }
    }
    
    输出:
    Sequential Stream : [G, E, E, K, S]
    

  2. 使用 Iterable.Spliterator() :Spliterator 是创建顺序流的关键。因此,在这种方法中,也使用了 Spliterator。但是在这种方法中,Spliterator 的源设置为从Iterator创建的Iterable

    所以首先 Iterable 是从 Iterator 创建的。然后 Spliterator 作为Iterable.spliterator()直接传递给stream()方法。

    程序:

    // Java program to create a Sequential Stream
    // from an Iterator
      
    import java.util.*;
    import java.util.stream.*;
      
    class GfG {
        // Function to create a sequential Stream
        // from an Iterator
        public static  Stream 
                      iteratorToSequentialStream(Iterator itr)
        {
            // Get an iterable from itr
            Iterable itb = () -> itr;
      
            // Get spliterator() from iterable and then
            // Convert into a sequential stream.
            // The second parameter "false" passess whether the
            // stream is to be created parallel or not
            return StreamSupport.stream(itb.spliterator(), false);
        }
      
        public static void main(String[] args)
        {
            Iterator iterator = Arrays.asList("G", "E", "E",
                                                "K", "S").iterator();
      
            Stream stream = iteratorToSequentialStream(iterator);
              
            System.out.println("Sequential Stream : " +
                        stream.collect(Collectors.toList()));
        }
    }
    
    输出:
    Sequential Stream : [G, E, E, K, S]