• 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
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Lambda Expressions

 
Ranch Hand
Posts: 42
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain me clearly about lambda expressions and predicate for OCAJP -8 preparation ? I need a guidance.
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be difficult, especially since we don't know your experience level or learning style. So perhaps you could start by pointing at some explanations you've already seen and explain what you found unclear about them?
 
Marshal
Posts: 64684
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start with these sections of the Java™ Tutorials.
 
Saloon Keeper
Posts: 3300
146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the OCAJP the use of Streams and lambdas is pretty limited. Make sure you are very familiar with the syntax, and be sure to know what a Predicate<T> means.
For instance, is: legal? And what about:
or

Expect questions like these.

In this topic an exercise from chapter 4 from the Sybex OCAJP (Boyarsky & Selikoff) is tackled: example
 
Shweta Priyadarshi
Ranch Hand
Posts: 42
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:For the OCAJP the use of Streams and lambdas is pretty limited. Make sure you are very familiar with the syntax, and be sure to know what a Predicate<T> means.
For instance, is: legal? And what about:
or

Expect questions like these.

In this topic an exercise from chapter 4 from the Sybex OCAJP (Boyarsky & Selikoff) is tackled: example



Thank you Piet Souris for the help i really appreciate.
 
Shweta Priyadarshi
Ranch Hand
Posts: 42
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Paul and Campbell for help.
 
Shweta Priyadarshi
Ranch Hand
Posts: 42
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am attaching the problem ...which is from Kathy Sierra book . I didn’t get it . Can you explain me ?
121DAE36-2EC5-4DF6-A6E8-A49315937DE9.jpeg
[Thumbnail for 121DAE36-2EC5-4DF6-A6E8-A49315937DE9.jpeg]
 
Shweta Priyadarshi
Ranch Hand
Posts: 42
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Other part of this problem
EEEACEC2-A606-4A2B-8AAB-E594EE3CF057.jpeg
[Thumbnail for EEEACEC2-A606-4A2B-8AAB-E594EE3CF057.jpeg]
 
Campbell Ritchie
Marshal
Posts: 64684
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always post text if posssible.
Apart from the misprint in ArrayList< >Dog>: please explain which part you don't understand, then we can give you an answer.
 
Shweta Priyadarshi
Ranch Hand
Posts: 42
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please always post text if posssible.
Apart from the misprint in ArrayList< >Dog>: please explain which part you don't understand, then we can give you an answer.



// declare a new lambda powered, generic, multi-purpose query method
static ArrayList< >Dog> dogQuery(ArrayList< >Dog> dogList, Predicate< >Dog> expr) {
// do an "on the fly" query
ArrayList< >Dog> result1 = new ArrayList< >>(); for(Dog d: dogList)
if(expr.test(d))
result1.add(d); return result1;
} }


 
Shweta Priyadarshi
Ranch Hand
Posts: 42
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

// declare a new lambda powered, generic, multi-purpose query method
static ArrayList< >Dog> dogQuery(ArrayList< >Dog> dogList, Predicate< >Dog> expr) {
// do an "on the fly" query
ArrayList< >Dog> result1 = new ArrayList< >>();
for(Dog d: dogList)
if(expr.test(d))
result1.add(d);
return result1;
} }

I didn’t. Get this part actually . And I don’t think so any part is missed . This is from Kathy Sierra book OCA- 8 preparation
 
Campbell Ritchie
Marshal
Posts: 64684
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That suggests you want to know what an “on the fly query” is.

Right: have you looked at the tutorial section I posted yesterday, or any other sort of tutorial? What does it say in the book about λs? Simply, the Java8 runtime allows you to create the equivalent of an instance of certain interfaces. Predicate is one of them, and if you look at that link, you will find it is tagged @FunctionalInterface, which you should read too. There it explains about one abstract method, so the compiler automatically knows which method you are trying to implement. In a λ, all you need to supply is the arguments to that one method, to the left of the ->, and the body of the method to the right of ->. You will also find that -> is called the arrow token, and that in most cases you can abbreviate both halves of the λ quite a lot.
 
Piet Souris
Saloon Keeper
Posts: 3300
146
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method declared has two arameters: a List of Dogs, and a Predicate<Dog> (by the way: note the typo in the declaration of this Predicate, as Campbell already pointed out).

Back in the pre java-8 era you would have to do something like this: (note: I use Strings here, since I don't have a suitable Dog class available, and also that Predicate was introduced in java 8. But it is just an example)

And having this method:

You could invoke it like this:


Now, java 8 enables to write all of this much easier, by using lambdas. So we could say:

But it can be done even shorter, by specifying the lambda directly as argument:

Now, all this is pretty much confusing in the beginning. But after practice, you start wondering how you ever managed without lambdas!

Therefore, keep practising!
 
Shweta Priyadarshi
Ranch Hand
Posts: 42
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Campbell & Piet for your valuable suggestions. Yes it require lot of practice . Kathy Sierra has questions which is little bit harder . I Am also consulting Jeanne Boyarsky and mala Gupta book so feeling better . And l’ll definitely go with the tutorial you sent to me.
Thanks again.
 
Piet Souris
Saloon Keeper
Posts: 3300
146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!

For ease of mind: I have been struggling too for quite some time, and if I could get a hang of it, you certainly will.
 
Shweta Priyadarshi
Ranch Hand
Posts: 42
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:You're welcome!

For ease of mind: I have been struggling too for quite some time, and if I could get a hang of it, you certainly will.




Thanks Piet for the encouragement.
Yes someday I will😊
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!