O'Reilly logo

Java 11 Cookbook by Mohamed Sanaulla, Nick Samoylov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. The int compare(T a, T b, Comparator<T> c) method uses the provided comparator for comparing the two objects:

    • Returns 0 when the objects are equal
    • Returns a negative number when the first object is smaller than the second one
    • Returns a positive number otherwise

The non-zero return value of the int compare(T a, T b, Comparator<T> c) method depends on the implementation. In the case of String, smaller and bigger are defined according to their ordering position (smaller is placed in front of bigger in the ordered list), and the returned value is the difference between the positions of the first and the second parameter in the list, ordered according to the provided comparator:

int res =      Objects.compare("a", "c", Comparator. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required