Java Stream of toStream(Enumeration e)

Here you can find the source of toStream(Enumeration e)

Description

Transform an Enumeration to a Stream

License

Open Source License

Parameter

Parameter Description
e a parameter
T a parameter

Return

A sequential stream

Declaration

public static <T> Stream<T> toStream(Enumeration<T> e) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class Main {
    /**//from   w  ww.j  av  a 2 s .com
     * Transform an Iterator to a Stream
     * @param sourceIterator
     * @param <T>
     * @return A sequential stream
     */
    public static <T> Stream<T> toStream(Iterator<T> sourceIterator) {
        return toStream(sourceIterator, false);
    }

    /**
     * Transform an Iterator to a Stream
     * @param sourceIterator
     * @param parallel true to create a parallel stream, false for sequential stream
     * @param <T>
     * @return A stream
     */
    public static <T> Stream<T> toStream(Iterator<T> sourceIterator, boolean parallel) {
        Iterable<T> iterable = () -> sourceIterator;
        return StreamSupport.stream(iterable.spliterator(), parallel);
    }

    /**
     * Transform an Enumeration to a Stream
     * @param e
     * @param <T>
     * @return A sequential stream
     */
    public static <T> Stream<T> toStream(Enumeration<T> e) {
        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(new Iterator<T>() {
            public T next() {
                return e.nextElement();
            }

            public boolean hasNext() {
                return e.hasMoreElements();
            }
        }, Spliterator.ORDERED), false);
    }
}

Related

  1. streamOrEmpty(Iterable iterable)
  2. streamScanner(Scanner scanner)
  3. streamToList(Stream stream)
  4. toParallelStream(Iterator iter)
  5. toStream(Collection collection)
  6. toStream(final Iterable iterable)
  7. toStream(final Iterable iterable)
  8. toStream(final T source)
  9. toStream(Iterable iterable)