This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

lambdas in Boyarsky and Selikoff book

 
Ben Pittens
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

In the Boyarsky and Seliikoff book at page 212 there is stated this code:



There is also written that the reader has to pretend some valid interfaces that can consume a lambda with zero, one or two String parameters.

With one parameter I can figure it out, the print method can be something like this:





But what if we have 2 variables, how does method print then look like?

Do you have a link for some more "hello world" lambda examples for starters with lambdas?

thanks
Kind regards
Ben
 
Scott Selikoff
author
Saloon Keeper
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at BiPredicate, I think it is what you are looking for. It takes two generic values, and returns a boolean.

In fact, Java 8 includes dozens of simple generic functional interfaces for various types of lambda expressions. For example, lambdas that return a type of a class reference, or take multiple parameters, etc. In fact, you can even write your own functional interfaces, then create lambda expressions for them. As long as the compiler has access to a matching functional interface, then it will accept the lambda expression.

For the OCA book, we only cover basic syntax and Predicate, because is what the exam requires. In our OCP book, though, we spent multiple chapters exploring functional interfaces, lambda expressions, and streams.
 
Ben Pittens
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Scott,

Untill now I didn't understand lambdas, I am just starting with lambda expressions.
Now I understand how the basics works. I am happy with that.

Maybe you can commit the BiPredicate print function as an example to the book. Just a tip.

OCP is next step, first OCA.

Kind regards
Ben
 
Scott Selikoff
author
Saloon Keeper
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Pittens wrote:Maybe you can commit the BiPredicate print function as an example to the book. Just a tip.


Appreciate the feedback, but BiPredicate is not part of the OCA exam. There are so many functional interfaces that we chose to focus on only the ones you need to know for the OCA exam. Don't worry, you'll see plenty of functional interfaces when studying for the OCP exam!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Scott. Adding extras invites "why did you include that functional interface and not that one." It's a slippery slope towards confusion.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Pittens wrote:But what if we have 2 variables, how does method print then look like?

When using lambdas the world is your oyster. And that's the beauty of this language feature. The only thing you need is a functional interface. In this topic all requirements for a functional interface are explained in great detail (with illustrative code snippets).

And here you'll have the TwoStrings functional interface (what's in a name? )And this functional interface can be used in the print method as illustrated in this code snippet defining two straightforward (easy) lambdas and a more complex oneOutput:
ABCDEvwxyz
ABCDEFvwxyz
ABCDEuvwxyz
zyxwvEDCBA
zyxwvFEDCBA
zyxwvuEDCBA
AvBwCxDyEz
AvBwCxDyEzF
AuBvCwDxEyz


Hope it helps!
Kind regards,
Roel
 
Ben Pittens
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks that helps a lot,

So I can also give it 3 params and even another interface calc with 4 params ike this?

I thinks I fully understand this now.



Gives:
ABCDEvwxyz123456
ABCDEFvwxyz1234
ABCDEuvwxyz12
36
8


 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Pittens wrote:So I can also give it 3 params and even another interface calc with 4 params ike this?

Sure! You can create a lambda expression from any functional interface. In the previous examples all parameters and the return type have the same type but that's not required. The sky is the limit!

Here's an example of an (insane) functional interface (it even has a generic type parameter)

Hope it helps!
Kind regards,
Roel

PS. The name of the (functional) interface is a reference to one of the characters of the awesome A-Team. He knows everything about insane
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic