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. ...

Get Java 11 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.