If I have a method that takes a single-method interface as an argument, I can call it like this:
foo(new Bar() {
@Override
public String baz(String qux) {
return modify(qux) + transmogrify(qux);
}
}
But if I have to call foo millions of times in a tight loop, I might prefer to avoid creating a new instance of the anonymous class every time through the loop:
final Bar bar = new Bar() {
@Override
public String baz(String qux) {
return modify(qux) + transmogrify(qux);
}
};
while (...) {
foo(bar);
}
Now if I replace the anonymous class with a lambda expression:
while (...) {
foo(qux -> modify(qux) + transmogrify(qux));
}
Is this lambda expression equivalent to the first or second snippet from the above examples of anonymous class?