In Java, path.equals(other) returns true when other is a Path of the same file system with the same root and the same name elements. It compares the paths as written; it does not open or look up any file.
Small spelling differences are already removed when a path is created, so Paths.get("docs//report.txt/") equals Paths.get("docs/report.txt"). Dots are not removed, and a relative path never equals an absolute one, even if both lead to the same file.
An instance method of the java.nio.file.Path interface, available since Java 7. Call it on a Path object, as in path.equals(...), after import java.nio.file.Path;.
Syntax
Compare two paths; the argument type is Object:
Java
Path a = Paths.get("docs", "report.txt");
Path b = Paths.get("docs/report.txt");
boolean same = a.equals(b); // true
boolean never = a.equals("docs/report.txt"); // false: a String is not a Path
Call
What it does
a.equals(b)
Same path text, name by name.
a.normalize().equals(b.normalize())
Same after removing . and ...
Files.isSameFile(a, b)
Same file on disk (follows links; the file must exist).
Since Java 7. equals is consistent with hashCode() and with compareTo, so paths work as keys in HashMap and TreeMap.
Call it on both sides first to ignore . and .. differences.
Java Examples
Outputs below are shown for Linux and macOS. On Windows, Java prints \ between names instead of /; the note above each output says when a result really differs.
Example 1 — Compare Different Spellings of the Same Path
Build the same report path in several ways and compare each with equals, including the String mistake.
Cleaned while parsing. Joining parts with Paths.get and writing extra or trailing slashes produce exactly the same path, so the first two comparisons are true.
A dot is a real name. docs/./report.txt has three names, so it differs from report; after normalize() the dot is gone and the paths are equal.
Wrong type, always false.equals("docs/report.txt") receives a String, and a Path only equals another Path; the compiler accepts it because the parameter is Object.
Example 2 — See How Case and Absolute Paths Affect equals
Compare names that differ only in case, and a relative path with its absolute form.
Run with java PathEqualsCase. Shown for Linux and macOS. On Windows the first line is true, because Windows paths are compared without regard to case; the other lines are the same.
different case: false
ignoring case: true
relative vs abs: false
both absolute: true
How It Works
Case counts on Linux and macOS. photos/beach.jpg and Photos/Beach.JPG are different paths there, so the first line is false; a case-insensitive text comparison makes them match.
Relative never equals absolute.toAbsolutePath() adds the working directory in front, so even though both describe the same location, the texts differ and equals says false.
Make both sides the same kind. Converting both paths with toAbsolutePath() gives equal results; only the boolean is printed because the working directory changes between computers.
Example 3 — Search a List of Paths the Right Way
List.contains and indexOf call equals for you. Searching with a String silently finds nothing.
Collections use equals.recent.get(1).equals(...) is true, and contains(Paths.get("docs", "b.txt")) runs exactly that Path.equals test on each element until one matches.
The String search compiles but fails.contains("docs/b.txt") accepts any Object; a String is never equal to a Path, so the answer is false without a warning.
Find and remove by value.indexOf reports position 2 for the song, and remove(Object) deletes the equal docs/a.txt entry, leaving two paths.
Frequently Asked Questions
Use a.equals(b) to compare the path text name by name. Normalize both paths first if they may contain "." or "..", and use Files.isSameFile(a, b) to ask whether they are the same file on disk.
equals only returns true for another Path. A String is a different type, so the result is always false. Compare path.toString() with the String, or wrap the String with Paths.get.
On Linux and macOS, yes, even when the macOS disk itself ignores case. On Windows, paths are compared without regard to case.
No. It compares the text only. Use normalize() for dots and toRealPath() or Files.isSameFile for links.
Yes. Path implements equals and hashCode consistently, and it is immutable, so it is a safe map key.