Find Duplicate Files By Size And Hash
Group files from a directory walk by a size-plus-SHA-256 fingerprint so only true byte-level duplicates survive the grouping.
Code
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class DuplicateFinder {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("dups-");
Files.writeString(dir.resolve("a.txt"), "same bytes");
Files.writeString(dir.resolve("b.txt"), "same bytes");
Files.writeString(dir.resolve("c.txt"), "different bytes");
try (Stream<Path> paths = Files.walk(dir)) {
Map<String, List<Path>> groups = paths
.filter(Files::isRegularFile)
.collect(Collectors.groupingBy(DuplicateFinder::fingerprint));
groups.values().stream()
.filter(group -> group.size() > 1)
.map(group -> group.stream().sorted().toList())
.forEach(group -> System.out.println("duplicates: " + group));
}
}
// file size gates the cheap path; the SHA-256 hash confirms byte equality
static String fingerprint(Path path) {
try {
byte[] hash = MessageDigest.getInstance("SHA-256")
.digest(Files.readAllBytes(path));
return Files.size(path) + ":" + HexFormat.of().formatHex(hash);
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
}
}
}What this snippet actually does — Find Duplicate Files By Size And Hash
Everything happens inside the class DuplicateFinder and the function fingerprint.
The transformation runs as a stream pipeline — source, intermediate operations, terminal collector — so each stage stays independently readable. Resources are declared in a try-with-resources header, so they close on every exit path without a finally block. Invalid states are rejected up front: it throws UncheckedIOException and IllegalStateException instead of continuing with bad data.
Points of care
- The stock toolchain runs it unchanged — add-ons are not part of the picture.
Putting it to use
The callable that defines the demo: static String fingerprint(Path path)
Output lines are prefixed (duplicates:), and the prefix tells you which step printed the line.
There are 1 call inside main — fingerprint — executed top to bottom.
Core lines
.collect(Collectors.groupingBy(DuplicateFinder::fingerprint));
.map(group -> group.stream().sorted().toList())
.forEach(group -> System.out.println("duplicates: " + group));
return Files.size(path) + ":" + HexFormat.of().formatHex(hash);
Interactive Tools for This Topic
FAQ
- Does the code validate its input?
- It throws `UncheckedIOException` and `IllegalStateException` before any real work runs — handle it at the call site or let it propagate deliberately.
- What does Find Duplicate Files By Size And Hash print when run?
- Running the file directly is enough — expect labeled output lines such as `duplicates:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.