I created a list and using the eclipse ODI when I checked what methods can be called on it, it shows forEach with parameter of type Consumer. Now based on this type consumer how do I know:
1)whether I can pass a function to it?
1) what kind of function I can pass to it?
If you lookup the Javadoc for Consumer you will see it has a functional interface. A Consumer function just takes something and does something with it. It can be a lambda or "method call" (I'm not sure I've got the terminology correct). It's easy to write a test program to study this.
Edit: what I was calling a "method call" is actually a "method reference."
All things are lawful, but not all things are profitable.
But I think something may be missing from the answers to the main question. Given that, yes, the method takes a Consumer, how do you know what kind of lambda expression can work here? That's not entirely clear. The thing is to look at the methods in the interface, and find the one method that has no implementation - i.e. it does not have the keywords "static" or "default" that would allow you to put an implementation in the interface. If there is no such method, or if there's more than one, then the compiler will not allow you to pass a lambda or method reference for this interface. But if there is exactly one, then you're good - that's what makes this a functional interface.
In the case of Consumer, there's an andThen() method, but it has a default implementation. And there's an accept() method, with no implementation. That's the one method that tells you what kind of lambda or method reference you can use here. You actually don't care about the name of the method at all here - "accept" is not something you need to use. But you do care about parameter types that it takes, and the return type.
This tells you that you need a method that takes one parameter, of type T, and returns nothing (void). In your code, you have a List<String>, so T is a String. That means you need a lambda that takes one String, and returns nothing. Possible examples:
You can also use a method reference, as long as that method takes a String, and returns void. Or it can be an instance method on String that takes no additional parameters, and returns void. Bad example here since there's no good method that fits the bill; we don't want to call finalize() for any reason. I'll drop the additional method reference possibilities for now; the main point was to understand how to tell what are the input params and return type for a lambda. Hope that helps...
posted 10 months ago
Good point about var in Java 10 Rob - I forgot because I mostly focus on the LTS releases, going from 8 to 11. Oops.
posted 10 months ago
I was thinking that this is the forEach of forEach loop. That is why it was more confusing. Now I realized that there is a forEach which can be called on iterable and has nothing to do with forEach loop and it is this one that can have the functional parameter.
Excellent post, Mike, but you've made one error. If the functional interface method returns void, you can use an expression that returns something, or use a reference to a method that returns something. The compiler will ignore the return value. For example: