Is there a method reference for a no-op (NOP) that can be used for anything lambda?
This is no deficiency.
Lambdas in Java are instances of functional interfaces; which, in turn, are abstracted to instances of Java constructs which can be simplified to one single abstract method, or SAM.
But this SAM still needs to have a valid prototype. In your case, you want to have a no-op Consumer<T>
which does nothing whatever the T
.
It still needs to be a Consumer<T>
however; which means the minimal declaration you can come up with is:
private static final Consumer<Object> NOOP = whatever -> {};
and use NOOP
where you need to.
In your particular case you could simply do:
foo(i -> {});
This means that the lambda expression receives a parameter but has no return value.
The equivalent code for a BiConsumer<T, U>
would be:
void bifoo(BiConsumer<Object, Object> consumer) { ... }
bifoo((a, b) -> {});
Could Function.identity()
fit your needs?
Returns a function that always returns its input argument.