Java Path equals() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

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

Library declarations
Java
// java.nio.file.Path (interface)
boolean equals(Object other)

Parameters

ParameterDescription
otherThe object to compare with. Anything that is not a Path (including a String or null) gives false.

Return Value

Return ValueDescription
trueother is an equal Path.
falseDifferent text, different type, or a path of another file system.

Compare with Similar Methods

Ways to compare paths:

MethodHow it differs
compareTo()Returns an order (negative, 0, positive) for sorting; 0 exactly when equals is true.
Files.isSameFile()Asks the file system whether two paths locate the same file.
normalize()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.

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

public class PathEqualsBasic {
    public static void main(String[] args) {
        Path report = Paths.get("docs/report.txt");
        System.out.println("parts:      " + report.equals(Paths.get("docs", "report.txt")));
        System.out.println("slashes:    " + report.equals(Paths.get("docs//report.txt/")));
        System.out.println("dot:        " + report.equals(Paths.get("docs/./report.txt")));
        System.out.println("normalized: " + report.equals(Paths.get("docs/./report.txt").normalize()));
        System.out.println("String:     " + report.equals("docs/report.txt"));
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

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

public class PathEqualsCase {
    public static void main(String[] args) {
        Path lower = Paths.get("photos/beach.jpg");
        Path upper = Paths.get("Photos/Beach.JPG");
        System.out.println("different case:   " + lower.equals(upper));
        System.out.println("ignoring case:    " + lower.toString().equalsIgnoreCase(upper.toString()));
        System.out.println("relative vs abs:  " + lower.equals(lower.toAbsolutePath()));
        System.out.println("both absolute:    " + lower.toAbsolutePath().equals(Paths.get("photos", "beach.jpg").toAbsolutePath()));
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Java
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PathEqualsList {
    public static void main(String[] args) {
        List<Path> recent = new ArrayList<>(Arrays.asList(
                Paths.get("docs/a.txt"), Paths.get("docs/b.txt"), Paths.get("music/song.mp3")));
        System.out.println("get(1).equals:   " + recent.get(1).equals(Paths.get("docs", "b.txt")));
        System.out.println("contains Path:   " + recent.contains(Paths.get("docs", "b.txt")));
        System.out.println("contains String: " + recent.contains("docs/b.txt"));
        System.out.println("indexOf song:    " + recent.indexOf(Paths.get("music/song.mp3")));
        System.out.println("remove a.txt:    " + recent.remove(Paths.get("docs/a.txt")));
        System.out.println("size now:        " + recent.size());
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Next: Path.getFileName()

Get the last name element of a path, such as report.txt, or null for a root.

getFileName() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful