I just noticed that Consumer
doesn't have an identity()
method, like java.util.function.Function
has.
Yes, it would just be a hole to drop stuff into, but at least it would be completely clear that I'm not just missing some code in the brackets.
Take this contrived example:
public void applyConsumerIfExists(String key, String param) {
Map<String, Consumer<String>> consumers = new HashMap<>();
consumers.put("a", MyClass::myConsumer);
// I can create my own, but that's no fun :(
Consumer<String> identity = input -> {};
consumers.getOrDefault(key, identity).accept(param);
// DOESN'T WORK, since identity() doesn't exist on Consumer
consumers.getOrDefault(key, Consumer.identity()).accept(param);
}
Question
Why doesn't Consumer
hava an identity
method?