• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Comparator comparing  RSS feed

 
Ranch Hand
Posts: 77
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
 
Saloon Keeper
Posts: 10101
212
  • 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 just had the craziest dream. This tiny ad was in it.
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!