• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Wanna learn Method References ? Here's my example

 
Bartender
Posts: 2880
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a part of learning Lambdas, I was looking at the different method references variants. The explanation at https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html was a bit confusing to me. So I came up with my own example. Hope this helps you.

Main class:

EmployeeAnalyzer class:

EmployeePrinter class:

Employee class:

Output

I am just learning stuff, any criticism is welcome.
 
Sheriff
Posts: 8064
569
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Method references basically fit to 2 rules I think:

1. When you invoke method on argument, i.e.:
so it can be replaced with:
or

2. When you pass singular argument to another method, either static or instance:So you can replace with:
And that is basically it. Alright, there are still those cases where you create an array let's say, but they I think they fit to one of those 2 examples.
 
Liutauras Vilda
Sheriff
Posts: 8064
569
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I think one more example worth including.

Let's assume you don't have instance created (writing API), and using internal private helper method, so you can use it with this, i.e.:

 
salvin francis
Bartender
Posts: 2880
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the cow, whoever it was.
 
salvin francis
Bartender
Posts: 2880
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:And I think one more example worth including...


It was worth including indeed. I modified the post to include it !
 
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi salvin,

this was a nice topic about method references: click here
 
salvin francis
Bartender
Posts: 2880
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:...this was a nice topic about method references...


I read that post and it inspired me to share this:

 
Marshal
Posts: 16597
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The filter() arguments you use have kind of a backwards perspective from what I would expect.

I would expect something more like this:

and so,

While it may help keep your example code shorter, I think that putting the filtering methods in the Main class gives a poor (and potentially misleading) context for the example. The reasons to avoid using a class like Main to hold your filtering methods are similar to the reasons why we advise beginners to have their Main class as a "driver" class for their actual program class, like a Game class, for example.

A better way to present it would probably be to have a separate EmployeeSelectors or EmployeeFilters class or just use Employee methods.

Here's a variant of your example (see https://repl.it/Mzt2/1)
 
Junilu Lacar
Marshal
Posts: 16597
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A separate Selector or Filter class would be warranted if you have somewhat specialized criteria, like "is a pirate", that you'd rather not have lumped in with the primary object. Let's say you don't feel like it's appropriate to have an Employee.isPirate() method or let's say you'd like to externalize the "business rule" of who is a senior vs. junior employee. You could have the special criteria in a separate but related class or enum:

You can use the EmployeeSelectors.INSTANCE like this:

In your Employee class, you eliminate the isSenior() method and replace it with a simple getYears() method. Now you have separated the logic/rules for "employee seniority" to a secondary employee-related class (or enum in this case)

You can try this version out here: https://repl.it/Mzt2/2
 
Junilu Lacar
Marshal
Posts: 16597
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And since the motivation was to separate the rule for employee seniority from the Employee domain object, I'd probably refactor(rename) that enum from EmployeeSelector to something like EmployeeSeniorityRule or something like that.

See https://repl.it/Mzt2/5

If you compare https://repl.it/Mzt2/5 with https://repl.it/Mzt2/2, you'll notice that no change had to be made to Employee when I refactored EmployeeSeniorityRule. This is a benefit you get from separating this kind of logic.

Be careful when you do this kind of thing though because overdoing it could result in an anemic domain model, one of the more dangerous liabilities of which is breaking of encapsulation. In this case, providing a getYear() method that is primarily intended for use by things like EmployeeSeniorityRule isn't too much of a concern since the design choice is intentional and consequences are limited.
 
salvin francis
Bartender
Posts: 2880
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Junilu,
Thanks for the feedback, I agree on not using Main class. I wanted to write a simple code that demonstrates:
  • Reference to an instance method of a particular object
  • Reference to an instance method of a particular object using 'this'
  • Reference to a static method
  • Reference to an instance method of an arbitrary object of a particular type
  • Reference to a constructor


  • Maybe I could have thought about a better example that's more practical instead of 'stuffing' it all into one example.
     
    Junilu Lacar
    Marshal
    Posts: 16597
    278
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yeah I thin examples that have realistic contexts provide better learning opportunity. Not only can you see how the syntax works but you can also see when you would use a particular technique.
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic