import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class EqualsHashCode {

    // Versi tanpa equals dan hashCode: identitas objek yang dipakai.
    static class TitikPlain {
        final int x;
        final int y;

        TitikPlain(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }

    // Versi dengan equals dan hashCode: isi field yang dipakai.
    static class Titik {
        final int x;
        final int y;

        Titik(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object o) {
            if (!(o instanceof Titik)) {
                return false;
            }
            Titik lain = (Titik) o;
            return x == lain.x && y == lain.y;
        }

        @Override
        public int hashCode() {
            return Objects.hash(x, y);
        }
    }

    // record: equals, hashCode, dan toString dibuat otomatis.
    record TitikRecord(int x, int y) { }

    public static void main(String[] args) {
        Map<TitikPlain, String> mapPlain = new HashMap<>();
        mapPlain.put(new TitikPlain(3, 4), "Budi");
        mapPlain.put(new TitikPlain(3, 4), "Siti");
        System.out.println("Tanpa equals/hashCode : ukuran map = " + mapPlain.size());
        System.out.println("  ambil (3,4)        : " + mapPlain.get(new TitikPlain(3, 4)));

        Map<Titik, String> map = new HashMap<>();
        map.put(new Titik(3, 4), "Budi");
        map.put(new Titik(3, 4), "Siti");
        System.out.println("Dengan equals/hashCode: ukuran map = " + map.size());
        System.out.println("  ambil (3,4)        : " + map.get(new Titik(3, 4)));

        Map<TitikRecord, String> mapRecord = new HashMap<>();
        mapRecord.put(new TitikRecord(3, 4), "Budi");
        mapRecord.put(new TitikRecord(3, 4), "Siti");
        System.out.println("Dengan record         : ukuran map = " + mapRecord.size());
        System.out.println("  ambil (3,4)        : " + mapRecord.get(new TitikRecord(3, 4)));
    }
}
