Java Utililty Methods Stream Operation

List of utility methods to do Stream Operation

Description

The list of methods to do Stream Operation are organized into topic(s).

Method

OptionalorStream(Stream> stream)
or Stream
return stream.filter(Optional::isPresent).findFirst().orElse(Optional.empty());
StreamparallelStreamFromIterable(Iterable in)
parallel Stream From Iterable
return StreamSupport.stream(in.spliterator(), true);
Streamprepend(final T first, final Stream rest)
Prepends a non-null first to stream rest , returning a new stream.
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);
IntStreamrangeStream(int n, boolean parallel)
range Stream
if (parallel)
    return IntStream.range(0, n).parallel();
else
    return IntStream.range(0, n);
SetreadStream(Stream s, boolean lowercase)
read Stream
return s.map(String::trim).filter(l -> !l.isEmpty()).filter(l -> !l.startsWith("#"))
        .map(l -> lowercase ? l.toLowerCase() : l).collect(Collectors.toSet());
StreamrebuildParallel(Stream stream)
Rebuilds a Stream into a Stream of the same type to enable a parallel Processing further on.
final Stream.Builder<T> streamBuilder = Stream.builder();
stream.forEach(streamBuilder);
return streamBuilder.build().parallel();
StringBuilderreplacePos(StringBuilder original, char replacementChar, Stream positionsToReplace)
Replaces each position in the original string with the replacement character.
positionsToReplace.forEach((index) -> original.setCharAt(index, replacementChar));
return original;
StringBuilderreplaceStr(StringBuilder original, char replacementChar, Stream stringsToReplace)
Replaces the occurrence of each string in stringsToReplace with the replacementChar in the original string.
stringsToReplace.forEach(toReplace -> {
    int index = -1;
    while ((index = original.indexOf(toReplace)) > -1) {
        for (int replacementIndex = index; replacementIndex < (index
                + toReplace.length()); replacementIndex++) {
            original.setCharAt(replacementIndex, replacementChar);
});
return original;
Streamreverse(Stream stream)
reverse
List<T> collection = stream.collect(Collectors.toList());
Iterator<T> iterator = new Iterator<T>() {
    private int index = collection.size();
    @Override
    public boolean hasNext() {
        return index > 0;
    @Override
...
Streamreverse(Stream input)
reverse
Object[] temp = input.toArray();
return (Stream<T>) IntStream.range(0, temp.length).mapToObj(i -> temp[temp.length - i - 1]);