Skip to content

Instantly share code, notes, and snippets.

@rschildmeijer
Created December 5, 2012 22:13
Show Gist options
  • Save rschildmeijer/4219988 to your computer and use it in GitHub Desktop.
Save rschildmeijer/4219988 to your computer and use it in GitHub Desktop.
Stream i/f from jdk8
/**
* A potentially infinite sequence of elements. A stream is a consumable data
* structure. The elements of the stream are available for consumption by either
* iteration or an operation. Once consumed the elements are no longer available
* from the stream.
*
* @param <T> Type of elements.
*
* @author Brian Goetz
*/
public interface Stream<T> extends BaseStream<T> {
Stream<T> filter(Predicate<? super T> predicate);
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
IntStream map(IntFunction<? super T> mapper);
<R> Stream<R> flatMap(FlatMapper<? super T, ? extends R> mapper);
// @@@ flatMapToInt
Stream<T> uniqueElements();
Stream<T> sorted(Comparator<? super T> comparator);
Stream<T> cumulate(BinaryOperator<T> operator);
/**
* Each element of this stream is processed by the provided block.
*
* @param block the Block via which all elements will be processed.
*/
void forEach(Block<? super T> block);
Stream<T> tee(Block<? super T> block);
/**
* Limit this stream to at most {@code n} elements. The stream will not be affected
* if it contains less than or equal to {@code n} elements.
*
*
* @param n the number elements the stream should be limited to.
* @return the truncated stream
*/
Stream<T> limit(long n);
/**
* Skip at most {@code n} elements.
*
*
* @param n the number of elements to be skipped.
* @return the truncated stream
*/
Stream<T> skip(long n);
/**
* Compute a subsequence of this stream
*
*
* @param skip the number of elements to be skipped.
* @param limit the maximum length of the resulting stream
* @return the truncated stream
*/
Stream<T> slice(long skip, long limit);
<A extends Destination<? super T>> A into(A target);
Object[] toArray();
<U> Map<U, Collection<T>> groupBy(Function<? super T, ? extends U> classifier);
<U, W> Map<U, W> reduceBy(Function<? super T, ? extends U> classifier,
Supplier<W> baseFactory,
Combiner<W, T, W> reducer,
BinaryOperator<W> combiner);
T reduce(T base, BinaryOperator<T> op);
Optional<T> reduce(BinaryOperator<T> op);
<U> U fold(Supplier<U> baseFactory,
Combiner<U, T, U> reducer,
BinaryOperator<U> combiner);
Optional<T> max(Comparator<? super T> comparator);
Optional<T> min(Comparator<? super T> comparator);
boolean anyMatch(Predicate<? super T> predicate);
boolean allMatch(Predicate<? super T> predicate);
boolean noneMatch(Predicate<? super T> predicate);
Optional<T> findFirst();
Optional<T> findAny();
/**
* Convert this stream, if a parallel stream, to a sequential stream.
*
* @return a sequential stream.
*/
Stream<T> sequential();
/**
* Convert this stream to a stream that has no encounter order.
*
* @return a stream whose output elements have no encounter order.
*/
Stream<T> unordered();
/**
* An aggregate that supports an {@code addAll(Stream)} operation.
*
* @param <T> Type of aggregate elements.
*/
interface Destination<T> {
public void addAll(Stream<? extends T> stream);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment