• 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

Comparator comparing

 
Ranch Hand
Posts: 79
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I am reading the OCP study guide by Scott Selikoff. On page 150 they briefly talk about a way to compare multiple fields using the Comparator<T> interface. The code snippet below is quoted from the book.


I understand lambda's until a certain extent so I get that the upper code can be written out in the following code


So I get that the Comparator.comparing(Function<T, R> f) method returns a Comparator object.

1) But what does it do behind the scene's?
2) What does the funcational interface Function (and apply method) do behind the scene's (I get that it order the String values specified in te getSpecies() method but how does it work?

thanks in advance !

Bart
 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your second code snippet isn't correct. It should read like this:

Bart Boersma wrote:1) But what does it do behind the scene's?


Something like this:

I replaced the type variables T and U with Squirrel and String to make it more clear in the context of your question, but the real method implementation of course works with generics so that it doesn't just work for squirrels and strings.

2) What does the funcational interface Function (and apply method) do behind the scene's (I get that it order the String values specified in te getSpecies() method but how does it work?


It transforms the object that you want to compare, but the application doesn't know how to compare (Squirrel) to an object that the application DOES know how to compare (String).
 
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic