This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Some doubts over using Predicate/Consumer  RSS feed

 
Vicky Roy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • 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: 63
  • Mark post as helpful
  • send pies
  • 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?
 
Pierre-Yves Saumont
Author
Ranch Hand
Posts: 156
28
  • Mark post as helpful
  • send pies
  • 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: 63
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!