Java Stream Operation iteratorToStream(final Iterator iterator)

Here you can find the source of iteratorToStream(final Iterator iterator)

Description

Convert an Iterator to a Stream

License

Apache License

Parameter

Parameter Description
iterator the iterator
T the type of the Stream

Return

the stream

Declaration

public static <T> Stream<T> iteratorToStream(final Iterator<T> iterator) 

Method Source Code


//package com.java2s;
/*/*from  www .  ja  v a  2  s .  co m*/
 * Copyright 2015 DuraSpace, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import static java.util.Spliterator.IMMUTABLE;
import static java.util.Spliterators.spliteratorUnknownSize;
import static java.util.stream.StreamSupport.stream;
import java.util.Iterator;
import java.util.stream.Stream;

public class Main {
    /**
     * Convert an Iterator to a Stream
     *
     * @param iterator the iterator
     * @param <T> the type of the Stream
     * @return the stream
     */
    public static <T> Stream<T> iteratorToStream(final Iterator<T> iterator) {
        return iteratorToStream(iterator, false);
    }

    /**
     * Convert an Iterator to a Stream
     *
     * @param <T> the type of the Stream
     * @param iterator the iterator
     * @param parallel whether to parallelize the stream
     * @return the stream
     */
    public static <T> Stream<T> iteratorToStream(final Iterator<T> iterator, final Boolean parallel) {
        return stream(spliteratorUnknownSize(iterator, IMMUTABLE), parallel);
    }
}

Related

  1. interleave(Stream a, Stream b)
  2. isEmpty(final Stream stream)
  3. isFinite(Stream stream)
  4. iterableOf(Stream stream)
  5. iteratorToFiniteStream(Iterator iterator, boolean parallel)
  6. lazyPartition(final Stream stream, final int maxPartitionSize)
  7. lazyStream(PrimitiveIterator.OfLong iterator)
  8. longArray(Stream stream)
  9. longList(LongStream stream)