Lexicographical comparison

If all we want is a lexicographical comparison of the paths, then we can rely on the Path.compareTo() method (this can be useful for sorting).

This method returns the following information:

  • 0 if the paths are equal
  • A value less than zero if the first path is lexicographically less than the argument path
  • A value greater than zero if the first path is lexicographically greater than the argument path:
int path1compareToPath2 = path1.compareTo(path2); // 0
int path1compareToPath3 = path1.compareTo(path3); // 24
int path2compareToPath3 = path2.compareTo(path3); // 24

Note that you may obtain different values than in the preceding example. Furthermore, in your business logic, it is important to rely on their meaning and not on their value (for example, say if(path1compareToPath3 > 0) { ... } and avoid if(path1compareToPath3 == 24) { ... }).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.145.82