List of utility methods to do Stream Operation
Optional | orStream(Stream or Stream return stream.filter(Optional::isPresent).findFirst().orElse(Optional.empty());
|
Stream | parallelStreamFromIterable(Iterable parallel Stream From Iterable return StreamSupport.stream(in.spliterator(), true);
|
Stream | prepend(final T first, final Stream 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); |
IntStream | rangeStream(int n, boolean parallel) range Stream if (parallel) return IntStream.range(0, n).parallel(); else return IntStream.range(0, n); |
Set | readStream(Stream read Stream return s.map(String::trim).filter(l -> !l.isEmpty()).filter(l -> !l.startsWith("#")) .map(l -> lowercase ? l.toLowerCase() : l).collect(Collectors.toSet()); |
Stream | rebuildParallel(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(); |
StringBuilder | replacePos(StringBuilder original, char replacementChar, Stream Replaces each position in the original string with the replacement character. positionsToReplace.forEach((index) -> original.setCharAt(index, replacementChar));
return original;
|
StringBuilder | replaceStr(StringBuilder original, char replacementChar, Stream 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; |
Stream | reverse(Stream extends T> 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 ... |
Stream | reverse(Stream reverse Object[] temp = input.toArray();
return (Stream<T>) IntStream.range(0, temp.length).mapToObj(i -> temp[temp.length - i - 1]);
|