Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Confused - Using Method References (p153 Boyarsky, Selikoff)

 
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm stumped by something concerning Method References that starts at the bottom of p153 of Boyarsky, Selikoff OCP Java SE 8 Programmer II Study Guide 2016 edition. It states:


..Let's look at some examples from the Java API.  In each set, we show the lambda equivalent.
..
..
Next up is calling an instance method on a specific instance:
Line 17 shows that we want to call string.startsWith() and pass a single parameter to be supplied at runtime. This would be a nice way of filtering the data in a list.


I do not understand how line 17 would do anything - how can it be the equivalent of the lambda version on line 18? Where is the equivalent of the lambda version's "s" argument?

My test class (below) shows how a Predicate that uses a lambda can be used to filter a list but I cannot work out how to achieve the same thing with a Method Reference.  So, if someone could explain/demonstrate what to insert in the commented section of the code it would be much appreciated.


Thanks,

John
 
Ranch Hand
Posts: 99
15
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the line 17 you create a method reference. You don't use it in this line. But you do say, you want to call the method startsWith on the string "abc".
The input parameter for this method reference can be put if you call the method test(String) on the Predicate, you created:

A lambda expression would make it the same:

For your example you need to create an other method:

And call it like this:
 
Viktor Logwin
Ranch Hand
Posts: 99
15
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way you write your lambda expression.

You say, you need a string parameter s and you need the string "Ab" to test this expression.
So for your example to work like you want, you need two parameters for your method - a string, on which the method startsWith should be called and a string for the input to the method startsWith.

So you need a BiPredicate:
 
John Simon
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Viktor

thanks for your reply.   Your suggestion of a separate method that defines the predicate in-line using the loop variable is a solution that I'd already come up with - I just wanted to be sure that there wasn't a way of doing it without having a separate method.

Regards,

John
 
Viktor Logwin
Ranch Hand
Posts: 99
15
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, the second idea is what you need.
 
John Simon
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Viktor Logwin wrote:Sorry, the second idea is what you need.


Yes, I wasn't aware of BiPredicate - but am now thanks to you.  I know my test class didn't do the same as the book example but it was the fact that the book said "This would be a nice way of filtering the data in a list." that got me thinking about it in the first place. I understand all of your answers but I'm still unsure as to what exactly the authors were getting at when they wrote the above - unless it was along the lines of your answers.
 
Viktor Logwin
Ranch Hand
Posts: 99
15
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And now you can call your method with any BiPredicate<String, String>
contains, endsWith, matches... even equals
 
Viktor Logwin
Ranch Hand
Posts: 99
15
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to say, the third chapter of the book was like hell for me. I was staring at this method references and didn't knew, what to do with it.
But in this chapter you only need to memorize, that there are alternatives to the lambda expressions. So you can write a lambda expression instead of a method reference. But the other way not always work. In the chapter 4 you will learn more built-in functional interfaces.

In my opinion, the method references sub-chapter shouldn't be there in the third chapter. It belongs to chapter 4. It makes much confusion.
 
Saloon Keeper
Posts: 12295
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that in their example, s is the second argument of startsWith(), while in your example s is the first argument. They can use a method reference on str, because str is declared outside the lambda, but you can't do the same for s, because s is declared inside the lambda.
 
John Simon
Greenhorn
Posts: 9
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Viktor Logwin wrote:I have to say, the third chapter of the book was like hell for me. I was staring at this method references and didn't knew, what to do with it.
But in this chapter you only need to memorize, that there are alternatives to the lambda expressions. So you can write a lambda expression instead of a method reference. But the other way not always work. In the chapter 4 you will learn more built-in functional interfaces.

In my opinion, the method references sub-chapter shouldn't be there in the third chapter. It belongs to chapter 4. It makes much confusion.



"...like hell", "..much confusion" - , yes method references are the same for me.  I'm hoping that at some point the Java language dev team have the sense to know when to stop always trying to infer from context and seeking to reduce the amount of code that has to be typed to near zero...
 
Viktor Logwin
Ranch Hand
Posts: 99
15
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Simon wrote:I'm hoping that at some point the Java language dev team have the sense to know when to stop always trying to infer from context and seeking to reduce the amount of code that has to be typed to near zero...


I just can't say it better. At the beginning of the exam preparation I was very enthusiastic about learning new Java features. I liked the Predicate interface and new DateTime API from the OCA exam and was looking forward for more useful new API components.
But now I think, that I just have to learn it, know how to determine correct syntax, clear the exam and forget the most topics. Sometimes I need to curse around. Java is my favorite programming language. It's nice for modelling a business context, the readability of code, written in Java, is very good. Why do I need to write less code and imagine me some contexts and make other programmer, who read my code, guessing what the code actually does?
I was playing around with streams and method references and I see, that the exception propagation doesn't work anymore. You can make some method chaining, but what happens, if something goes wrong? I'd better write some more loops and method calls, but have a good exception handling mechanism working.
Very sad.
 
reply
    Bookmark Topic Watch Topic
  • New Topic