• 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
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

From Comparator<T> interface how does compare() abstract method implement its behavior?

 
Ranch Hand
Posts: 62
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys. Could someone help me with something? I don't understand how does compare() abstract method implement the behavior of comparing both of those objects by their names? (according to my example). For sure, this information is given by the object that implements Function, but what i mean is, where is the body of compare() method that extracts this from that  object?

I'm in the end of the chapter Generic an Collections from ocp study guide and i wrote the example bellow inspired in a code given by the book (page 149 OCP Study Guide - Jeanne and Scott).

I was looking the API documentation and I understood how comparing() works by receiving an implementation of the interface Function and returning a Comparator.
* https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#compare-T-T-
* https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html

It's not easy for me to be clear in this point, for this I've done my own example to be able to visualize what I don't understand and in which point I stuck.



I think one way to understand it is looking the implementation of the interface Comparator to see the body of the comparing() static method but I don't know from where I can see it.

I really hope have been clear and thanks for read all of this text.
 
Bartender
Posts: 3519
150
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, a method that I can imagine is something like:

This is possible here since the String class implements Comparable<String>.
What if instead of String, you had a type that does not implement Comparable? See the API, you have to add a Comparator for that type as well.
 
Pablo Napoli
Ranch Hand
Posts: 62
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet, thanks for your reply . But I'm not sure about that method because that lambda is returning an int value while the comparing() method needs to return a Comparator object. Anyway do you know some way to find the implementation of this (or others) methods? Because in the API just I can find the related documentation.
 
Piet Souris
Bartender
Posts: 3519
150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My method returns a Comparator, not an int. I showed it so that you have another example of lambdas and Comparators, since that is always handy for the OCPJP.

In the lib-folder of your java installation you can find a file called src.zip, that contains the source code.
 
Pablo Napoli
Ranch Hand
Posts: 62
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I say that the method returns an int value because the result of the lambda should be an int due to compareTo of String returns int. Although maybe there is something that I'm not being able to see. Anyway I'm gonna look into that folder.
 
Pablo Napoli
Ranch Hand
Posts: 62
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh sorry. It's like an inner anonymous class and the method is not returning an int. I think I'm enough tired for today haha.
 
Piet Souris
Bartender
Posts: 3519
150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Me too. Good night and sleep well!
 
The moustache of a titan! The ad of a flea:
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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!