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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.