Java Stream of toStreamNumber(final Scanner scanner)

Here you can find the source of toStreamNumber(final Scanner scanner)

Description

to Stream Number

License

Open Source License

Declaration

public static Stream<Long> toStreamNumber(final Scanner scanner) 

Method Source Code


//package com.java2s;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Scanner;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class Main {

    public static Stream<Long> toStreamNumber(final Scanner scanner) {
        Objects.requireNonNull(scanner);

        final Iterator<Long> iter = new Iterator<Long>() {
            final Scanner s = scanner;

            @Override//from w w w  .  j a v a2s.c o m
            public boolean hasNext() {
                boolean hasNumber = false;

                while (s.hasNext()) {
                    hasNumber = s.hasNextLong();
                    if (hasNumber) {
                        return true;
                    }
                    s.next();
                }

                return false;
            }

            @Override
            public Long next() {
                if (!hasNext()) {
                    throw new NoSuchElementException();
                }
                return s.nextLong();
            }

        };
        return StreamSupport.stream(
                Spliterators.spliteratorUnknownSize(iter, Spliterator.ORDERED | Spliterator.NONNULL), false);
    }
}

Related

  1. toStream(Iterable iterable)
  2. toStream(Iterator iter, boolean parallel)
  3. toStream(Optional value)
  4. toStream(T[] array, boolean parallel)
  5. toStreamLine(final Scanner scanner)