Records are value-based, so equals comes free

Two records with equal components are equal and hash the same, which makes distinct() and Set membership behave the way you expect.

Code
record Point(int x, int y) {}

var points = List.of(new Point(1, 1), new Point(2, 2), new Point(1, 1));

System.out.println("distinct = " + points.stream().distinct().toList());
System.out.println("set size = " + new HashSet<>(points).size());
System.out.println("equals   = " + new Point(1, 1).equals(new Point(1, 1)));
System.out.println("same hash= " + (new Point(1, 1).hashCode() == new Point(1, 1).hashCode()));
Output
distinct = [Point[x=1, y=1], Point[x=2, y=2]]
set size = 2
equals   = true
same hash= true
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-07-30