This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Comparator comparing  RSS feed

 
Ranch Hand
Posts: 66
3
  • Mark post as helpful
  • send pies
  • 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
 
Bartender
Posts: 9493
184
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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).
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
Download Free Java APIs to Work with Office Files and PDF
htttp://www.e-iceblue.com/free-apis.html
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!