I have a list of runnables that I would like to call using lambda expressions:
Arrays.asList(runnable1, runnable2, runnable3, ...).forEach(r->r.run());
Is there a 'better' (more efficient) shortcut to call the Runnable
s run()
method other than the following way?
Arrays.asList(runnable1, runnable2, runnable3, ...).forEach(Runnable::run);
I think this expression will be translated to a Runnable
wrapping the runnable instance in the list.
EDIT:
My assumption/concern (maybe wrong) is that the compiler will translate the expression list.forEach(Runnable::run)
to something like this, and thus not 'efficient':
list.forEach(r -> new Runnable() {
@Override
public void run() {
r.run();
}
});