Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

How to know what kind of function forEach method of a list will take when it shows as Consumer?

 
Ranch Hand
Posts: 1258
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks
 
Marshal
Posts: 65842
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haven't you answered your own question already?
 
Sheriff
Posts: 6270
167
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
Marshal
Posts: 14070
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As of Java 9, you can also do this:
 
Rancher
Posts: 3373
31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For that matter, in Java 9 you can also do this:
 
Sheriff
Posts: 21805
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not true, var was introduced in Java 10.
 
Mike Simmons
Rancher
Posts: 3373
31
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Mike Simmons
Rancher
Posts: 3373
31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point about var in Java 10 Rob - I forgot because I mostly focus on the LTS releases, going from 8 to 11.  Oops.
 
Monica Shiralkar
Ranch Hand
Posts: 1258
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Posts: 21805
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Mike Simmons
Rancher
Posts: 3373
31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I meant to say you want a lambda expression that does not explicitly return anything (with return).  Instead I said you want a method that returns nothing... which is not correct.  Oops again.
 
Monica Shiralkar
Ranch Hand
Posts: 1258
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I checked for Consumer and it says that it is a Functional interface which takes 1 argument and returns no value. So that is good enough for System.out.println.
 
Campbell Ritchie
Marshal
Posts: 65842
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:. . . Consumer . . . is good enough for System.out.println.

Have you tried it?
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!