• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Some doubts over using Predicate/Consumer

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(from OCP book of Jeanne Boyarsky and Scott Selikoff)

It is on using Prdicates/Consumer that made me confused. I made my understanding that I need to pass the arguments in lambda expression that method implementation is supposed to use.

example 1: Here, since it is static method we are passing argument 'l'.

example 2: Here, we are passing only 's' not 'str'!!! Don't we need to pass 'str' also? How we will call it?

example 3: Here, we are passing only 's'!!! How we will call it?


Thanks,
Vicky.
 
Vicky Roy
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vicky Roy wrote:(from OCP book of Jeanne Boyarsky and Scott Selikoff)

It is on using Prdicates/Consumer that made me confused. I made my understanding that I need to pass the arguments in lambda expression that method implementation is supposed to use.

example 1: Here, since it is static method we are passing argument 'l'.

example 2: Here, we are passing only 's' not 'str'!!! Don't we need to pass 'str' also? How we will call it?

example 3: Here, we are passing only 's'!!! How we will call it?


Thanks,
Vicky.



I am not able to think their use cases? Which arguments I need to pass? Will Predicate be called like instance methods?
 
Author
Posts: 161
31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

example 1: Here, since it is static method we are passing argument 'l'. ?


Nothing static here. You may call it like this:

to get the list sorted.




Here, you have a closure over a variable named str of type String. You may call it as follows:

The third one is obvious.

Note that although you may declare and call lambdas as indicated, it makes no sense. Generally, lambdas will be used as parameters for methods or functions. Also note that when you write:

you are creating an object of type Predicate. This is equivalent to:

Lambdas are not primarily intended to create objects, but to be use anonymously, such as:

In such a case, no Predicate instance is ever created. But the most efficient way to write this is to use a method reference:

 
Vicky Roy
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pierre-Yves Saumont wrote:

example 1: Here, since it is static method we are passing argument 'l'. ?


Nothing static here. You may call it like this:

to get the list sorted.




Here, you have a closure over a variable named str of type String. You may call it as follows:

The third one is obvious.

Note that although you may declare and call lambdas as indicated, it makes no sense. Generally, lambdas will be used as parameters for methods or functions. Also note that when you write:

you are creating an object of type Predicate. This is equivalent to:

Lambdas are not primarily intended to create objects, but to be use anonymously, such as:

In such a case, no Predicate instance is ever created. But the most efficient way to write this is to use a method reference:



Thanks for the explanation but still I didn't get it. Can you explain a more verbose way.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you understand better if we rewrite the lambdas as regular classes:
These classes will do exactly the same as the lambdas, but they are of course a lot more verbose.
 
To do a great right, do a little wrong - shakepeare. twisted little ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic