Java - Paths Comparing for sequence

Introduction

The Path interface implements the java.lang.Comparable interface.

You can use its compareTo() method to compare it with another Path object textually.

The compareTo() method returns an int value, which is 0, less than 0, or greater than 0, when the two paths are equal, the path is less than the specified path, or the path is greater than the specified path, respectively.

The ordering used by this method to compare two paths is platform-dependent.

The following code shows examples of using the compareTo() method on Windows:

Demo

import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
  public static void main(String[] args) {
    Path p1 = Paths.get("C:\\myData\\Main.java");
    Path p2 = Paths.get("C:\\TEST\\Data1.txt");
    Path p3 = Paths.get("C:\\myData\\..\\myData\\Main.java");
    int v1 = p1.compareTo(p2); 
    System.out.println(v1);//from w w  w.  j  ava  2  s  .c o m
    int v2 = p1.compareTo(p3); 
    System.out.println(v2);
  }
}

Result

Related Topic