Search code examples
javafunctional-programmingjava-8java-streamlambda-calculus

How does "Stream" in java8 work?


I'm ask for the reason or the principle.

I found that most methods of the interface "Stream" in Java8 are not declared as "default",so they have no method body.For example:

boolean anyMatch(Predicate predicate);

boolean allMatch(Predicate predicate);

Stream map(Function mapper);

As you can see in the source file Stream.java.

But these methods without bodies seem to be able to be executed in java programs.

Do you know why?

Thank you.


Solution

  • abstract class ReferencePipeline<P_IN, P_OUT>
        extends AbstractPipeline<P_IN, P_OUT, Stream<P_OUT>>
        implements Stream<P_OUT>  ...
    

    It's ReferencePipeline that implements them. For example:

    @Override
    public final boolean anyMatch(Predicate<? super P_OUT> predicate) {
        return evaluate(MatchOps.makeRef(predicate, MatchOps.MatchKind.ANY));
    }