Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming 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

lambdas in Boyarsky and Selikoff book

 
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
 
author
Posts: 4112
28
Google Web Toolkit Eclipse IDE Flex
  • 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
Posts: 4112
28
Google Web Toolkit Eclipse IDE Flex
  • 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!
 
author & internet detective
Posts: 39528
776
Eclipse IDE VI Editor Java
  • 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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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
 
Live ordinary life in an extraordinary way. Details embedded in this 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!