What are the differences between an aggregate operation and a method?

231 Views Asked by At

..........

well, let me tell you i made some mistakes: the foreach() i was refering to is not an aggregate operation but a method from Iteable. I've changed the title of my question and its content.

My interest is to know if an aggregate operation is implemented by default and Where i can find that implementation.

1

There are 1 best solutions below

2
On

If you want to dig into the JDK sources you can always download them here as @jbkm suggest. If you have the Oracle JDK installed, there should be src.zip file in the installation folder which contains all the public sources. Alternatively you can check the OpenJDK sources online, for example, on GrepCode. As you can see, the implementation of forEach for Iterable is quite simple:

default void forEach(Consumer<? super T> action) {
    Objects.requireNonNull(action);
    for (T t : this) {
        action.accept(t);
    }
}

If you're asking about the implementation of forEach in the Stream API, it's much more tricky thing as it should evaluate all the previous pipeline steps and perform the operation in parallel for parallel streams. You can start your investigation examining the ReferencePipeline class which is the Stream implementation in OpenJDK.