Before Java 8, lambda functionality could be achieved by using anonymous inner classes. For example:
interface Lambda {
void doStuff();
}
// ...
public void doWithCallback(Lambda callback) {
// ...
callback.doStuff();
}
// ...
doWithCallback(new Lambda {
public void doStuff() {
// ...
}
});
In terms of performance, is there a difference between still using this approach and using the new Java 8 lambdas?
Oracle has posted a study comparing performance between Lambdas and anonymous classes
See JDK 8: Lambda Performance Study by Sergey Kuksenko, which is 74 slides long.
Summary: slow to warm up but when JIT inlines it worst case just as fast as anonymous class but can be faster.
As I found, the iterating over array with Stream is working much slower (74 slides are not consider such the case). I think that it is not the only performance leaks in lambdas (guess, it will be improved in the future). The example below was running with Java 8 without any options: