Skip to content

Instantly share code, notes, and snippets.

@dlhartveld
Created February 27, 2013 16:25
Show Gist options
  • Save dlhartveld/5049226 to your computer and use it in GitHub Desktop.
Save dlhartveld/5049226 to your computer and use it in GitHub Desktop.
Snippet from the JDK8 Collection interface demonstrating the addition of a default stream() method.
public interface Collection<E> extends Iterable<E> {
// Traditional, abstract methods:
int size();
boolean contains(Object o);
boolean add(E e);
boolean remove(Object o);
// Etc...
// New methods stream() and parallelStream() with default implementations:
default Stream<E> stream() {
return Streams.stream(() -> Streams.spliterator(iterator(), size(), Spliterator.SIZED), Spliterator.SIZED);
}
default Stream<E> parallelStream() {
return stream().parallel();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment