• 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

Method reference question

 
Greenhorn
Posts: 21
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to understand why the following output still prints "Reference" as part of the list:



I am mainly trying to understand method references, and I understand what the lambda version is doing. How can I use method reference to remove "Reference" from the list?

Thanks.
 
author & internet detective
Posts: 41905
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John,
Good question! That method reference does the opposite of what you want. By contrast, this code actually removes "Reference" from the list:



The key is that the String you are giving the method reference when you create it is treated as the instance variable in s.startsWith(x). Whereas the String from the list that removeIf is passing to your method reference is the parameter that is passed to startsWith().

Not every lambda can be rewritten as a method reference which makes it important to understand what is going on.
 
John Freeman
Greenhorn
Posts: 21
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeanne,

Thanks for the reply. I tried your code, and it does indeed remove "Reference" from the list. However, what I noticed is that in your example, rather than declaring the instance string separately as I did in my post, you included it right in the reference method call at line 6 of your code. So it seems that the difference between our code boils down to the fact that you used "Reference Library" as your instance string, while I used "R" in mine. When I changed mine to "Reference Library" it was removed in my list also.

If I understood your explanation correctly, "Reference Library"::startsWith translates to "Reference Library".startsWith("Reference Library") in traditional Java syntax. I don't understand how changing it to just "R" caused it to behave unexpectedly, but it worked for "Reference Library"
 
Jeanne Boyarsky
author & internet detective
Posts: 41905
909
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Freeman wrote: So it seems that the difference between our code boils down to the fact that you used "Reference Library" as your instance string, while I used "R" in mine.


Correct. That's because "Reference Library" starts with "Reference", but "R" does not start with "Reference"

John Freeman wrote: If I understood your explanation correctly, "Reference Library"::startsWith translates to "Reference Library".startsWith("Reference Library") in traditional Java syntax. I don't understand how changing it to just "R" caused it to behave unexpectedly, but it worked for "Reference Library"


No. "Reference Library"::startsWith translates to "Reference Library".startsWith("Reference"). or "Reference Library"::startsWith("AnotherValueInYourList")
 
John Freeman
Greenhorn
Posts: 21
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:

John Freeman wrote: So it seems that the difference between our code boils down to the fact that you used "Reference Library" as your instance string, while I used "R" in mine.


Correct. That's because "Reference Library" starts with "Reference", but "R" does not start with "Reference"

John Freeman wrote: If I understood your explanation correctly, "Reference Library"::startsWith translates to "Reference Library".startsWith("Reference Library") in traditional Java syntax. I don't understand how changing it to just "R" caused it to behave unexpectedly, but it worked for "Reference Library"


No. "Reference Library"::startsWith translates to "Reference Library".startsWith("Reference"). or "Reference Library"::startsWith("AnotherValueInYourList")



Thanks Jeanne, I think I finally get it now.
 
Good night. Drive safely. Here's a tiny ad for the road:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic