Java Stream Operation prepend(final T first, final Stream rest)

Here you can find the source of prepend(final T first, final Stream rest)

Description

Prepends a non-null first to stream rest , returning a new stream.

License

Open Source License

Parameter

Parameter Description
first a parameter
rest a parameter

Declaration

public static <T> Stream<T> prepend(final T first, final Stream<T> rest) 

Method Source Code

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

import java.util.stream.Stream;

public class Main {
    public static final String ERR_FIRST_STREAM_ELEM_CANNOT_BE_NULL = "First stream element cannot be null.";

    /**//  ww w. ja  v  a  2s  .  co  m
     * Prepends a non-null {@code first} to stream {@code rest}, returning a new stream.
     * 
     * @param first
     * @param rest
     * @return
     */
    public static <T> Stream<T> prepend(final T first, final Stream<T> rest) {
        if (first == null) {
            throw new NullPointerException(ERR_FIRST_STREAM_ELEM_CANNOT_BE_NULL);
        }
        final Stream<T> xs = Stream.of(first);
        return Stream.concat(xs, rest);
    }

    /**
     * Creates a new stream from the provided values.
     * 
     * @param first
     * @param rest
     * @return
     */
    public static <T> Stream<T> of(final T first, final T... rest) {
        if (first == null) {
            throw new NullPointerException(ERR_FIRST_STREAM_ELEM_CANNOT_BE_NULL);
        }
        final Stream<T> xs = Stream.of(first);
        final Stream<T> ys = Stream.of(rest);
        return Stream.concat(xs, ys);
    }
}

Related

  1. opt2stream(Optional opt)
  2. optionalToStream(final Optional optional)
  3. optionalToStream(Optional optional)
  4. orStream(Stream> stream)
  5. parallelStreamFromIterable(Iterable in)
  6. rangeStream(int n, boolean parallel)
  7. readStream(Stream s, boolean lowercase)
  8. rebuildParallel(Stream stream)
  9. replacePos(StringBuilder original, char replacementChar, Stream positionsToReplace)