From d2f9719c6fbf71cb4dd0f854e053495c1a879f6c Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 4 Oct 2015 22:19:54 +0200 Subject: [PATCH] equals-hashcode: what is it for (HashSet relation) --- .../src/de/example/equalshash/Car.java | 34 ++++ .../de/example/equalshash/CarConstantHashCode.java | 34 ++++ .../de/example/equalshash/CarWithoutEquals.java | 25 +++ .../de/example/equalshash/CarWithoutHashCode.java | 29 ++++ .../de/example/equalshash/EqualsHashMainTest.java | 180 +++++++++++++++++++++ 5 files changed, 302 insertions(+) create mode 100644 Allgemeines/Equals-HashCode/src/de/example/equalshash/Car.java create mode 100644 Allgemeines/Equals-HashCode/src/de/example/equalshash/CarConstantHashCode.java create mode 100644 Allgemeines/Equals-HashCode/src/de/example/equalshash/CarWithoutEquals.java create mode 100644 Allgemeines/Equals-HashCode/src/de/example/equalshash/CarWithoutHashCode.java create mode 100644 Allgemeines/Equals-HashCode/src/de/example/equalshash/EqualsHashMainTest.java diff --git a/Allgemeines/Equals-HashCode/src/de/example/equalshash/Car.java b/Allgemeines/Equals-HashCode/src/de/example/equalshash/Car.java new file mode 100644 index 0000000..579da54 --- /dev/null +++ b/Allgemeines/Equals-HashCode/src/de/example/equalshash/Car.java @@ -0,0 +1,34 @@ +package de.example.equalshash; + +import java.util.Objects; + + +public class Car { + + private final int id; + private final String brand; + + public Car(int id, String brand) { + this.id = id; + this.brand = brand; + } + + @Override + public int hashCode() { + return Objects.hash(this.id, this.brand); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Car other = (Car) obj; + + return Objects.equals(this.id, other.id) && + Objects.equals(this.brand, other.brand); + } +} diff --git a/Allgemeines/Equals-HashCode/src/de/example/equalshash/CarConstantHashCode.java b/Allgemeines/Equals-HashCode/src/de/example/equalshash/CarConstantHashCode.java new file mode 100644 index 0000000..2f5680a --- /dev/null +++ b/Allgemeines/Equals-HashCode/src/de/example/equalshash/CarConstantHashCode.java @@ -0,0 +1,34 @@ +package de.example.equalshash; + +import java.util.Objects; + + +public class CarConstantHashCode { + + private final int id; + private final String brand; + + public CarConstantHashCode(int id, String brand) { + this.id = id; + this.brand = brand; + } + + @Override + public int hashCode() { + return 666; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final CarConstantHashCode other = (CarConstantHashCode) obj; + + return Objects.equals(this.id, other.id) && + Objects.equals(this.brand, other.brand); + } +} diff --git a/Allgemeines/Equals-HashCode/src/de/example/equalshash/CarWithoutEquals.java b/Allgemeines/Equals-HashCode/src/de/example/equalshash/CarWithoutEquals.java new file mode 100644 index 0000000..c4c0866 --- /dev/null +++ b/Allgemeines/Equals-HashCode/src/de/example/equalshash/CarWithoutEquals.java @@ -0,0 +1,25 @@ +package de.example.equalshash; + +import java.util.Objects; + + +public class CarWithoutEquals { + + private final int id; + private final String brand; + + public CarWithoutEquals(int id, String brand) { + this.id = id; + this.brand = brand; + } + + @Override + public int hashCode() { + return Objects.hash(this.id, this.brand); + } + + /** + * If not implementing equals it will be used the one implemented by Object class. + * The equals implemented by Object class depends on the instance pointer. + */ +} diff --git a/Allgemeines/Equals-HashCode/src/de/example/equalshash/CarWithoutHashCode.java b/Allgemeines/Equals-HashCode/src/de/example/equalshash/CarWithoutHashCode.java new file mode 100644 index 0000000..256247e --- /dev/null +++ b/Allgemeines/Equals-HashCode/src/de/example/equalshash/CarWithoutHashCode.java @@ -0,0 +1,29 @@ +package de.example.equalshash; + +import java.util.Objects; + + +public class CarWithoutHashCode { + + private final int id; + private final String brand; + + public CarWithoutHashCode(int id, String brand) { + this.id = id; + this.brand = brand; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final CarWithoutHashCode other = (CarWithoutHashCode) obj; + + return Objects.equals(this.id, other.id) && + Objects.equals(this.brand, other.brand); + } +} diff --git a/Allgemeines/Equals-HashCode/src/de/example/equalshash/EqualsHashMainTest.java b/Allgemeines/Equals-HashCode/src/de/example/equalshash/EqualsHashMainTest.java new file mode 100644 index 0000000..cd7bf3d --- /dev/null +++ b/Allgemeines/Equals-HashCode/src/de/example/equalshash/EqualsHashMainTest.java @@ -0,0 +1,180 @@ +package de.example.equalshash; + +import java.util.HashSet; + + +public class EqualsHashMainTest { + + public static void main(String[] args) { + + Car carA = new Car(1, "carA"); + Car carB = new Car(2, "carB"); + Car carC = new Car(3, "carC"); + Car carAA = new Car(1, "carA"); + + HashSet cars = new HashSet<>(); + + cars.add(null); + cars.add(carA); + cars.add(carB); + + if (cars.contains(carC)) { + System.out.println("contains carC"); + } else { + System.out.println("cars doesn't contain carC"); + } + + if (cars.contains(carAA)) { + System.out.println("cars contains carAA"); + } else { + System.out.println("cars doesn't contain carAA"); + } + + if (carA.equals(carAA)) { + System.out.println("carA == carAA"); + } else { + System.out.println("carA != carAA"); + } + + System.out.println("cars size before add carAA"); + System.out.println("cars size: " + cars.size()); + + System.out.println("cars size after add carAA"); + cars.add(carAA); + + System.out.println("cars size: " + cars.size()); + + + /** + * without hashCode: + * + * HashSet doesn't work + * + */ + CarWithoutHashCode carWithoutHashCodeA = new CarWithoutHashCode(1, "carWithoutHashCodeA"); + CarWithoutHashCode carWithoutHashCodeB = new CarWithoutHashCode(2, "carWithoutHashCodeB"); + CarWithoutHashCode carWithoutHashCodeC = new CarWithoutHashCode(3, "carWithoutHashCodeC"); + CarWithoutHashCode carWithoutHashCodeAA = new CarWithoutHashCode(1, "carWithoutHashCodeA"); + + HashSet carsWithoutHashCode = new HashSet<>(); + + carsWithoutHashCode.add(null); + carsWithoutHashCode.add(carWithoutHashCodeA); + carsWithoutHashCode.add(carWithoutHashCodeB); + + if (carsWithoutHashCode.contains(carWithoutHashCodeC)) { + System.out.println("carsWithoutHashCode contains carWithoutHashCodeC"); + } else { + System.out.println("carsWithoutHashCode doesn't contain carWithoutHashCodeC"); + } + + if (carsWithoutHashCode.contains(carWithoutHashCodeAA)) { + System.out.println("carsWithoutHashCode contains carWithoutHashCodeAA"); + } else { + System.out.println("carsWithoutHashCode doesn't contain carWithoutHashCodeAA"); + } + + if (carWithoutHashCodeA.equals(carWithoutHashCodeAA)) { + System.out.println("carWithoutHashCodeA == carWithoutHashCodeAA"); + } else { + System.out.println("carWithoutHashCodeA != carWithoutHashCodeAA"); + } + + System.out.println("carsWithoutHashCode size before add carWithoutHashCodeAA"); + System.out.println("carsWithoutHashCode size: " + carsWithoutHashCode.size()); + + System.out.println("carsWithoutHashCode size after add carWithoutHashCodeAA"); + carsWithoutHashCode.add(carWithoutHashCodeAA); + + System.out.println("carsWithoutHashCode size: " + carsWithoutHashCode.size()); + + + /** + * constant hashCode: + * + * Works but there is no good distribution in the HashSet. It is like having an Array :( + * + */ + CarConstantHashCode carConstantHashCodeA = new CarConstantHashCode(1, "carConstantHashCodeA"); + CarConstantHashCode carConstantHashCodeB = new CarConstantHashCode(2, "carConstantHashCodeB"); + CarConstantHashCode carConstantHashCodeC = new CarConstantHashCode(3, "carConstantHashCodeC"); + CarConstantHashCode carConstantHashCodeAA = new CarConstantHashCode(1, "carConstantHashCodeA"); + + HashSet carsConstantHashCode = new HashSet<>(); + + carsConstantHashCode.add(null); + carsConstantHashCode.add(carConstantHashCodeA); + carsConstantHashCode.add(carConstantHashCodeB); + + if (carsConstantHashCode.contains(carConstantHashCodeC)) { + System.out.println("carsConstantHashCode contains carConstantHashCodeC"); + } else { + System.out.println("carsConstantHashCode doesn't contain carConstantHashCodeC"); + } + + if (carsConstantHashCode.contains(carConstantHashCodeAA)) { + System.out.println("carsConstantHashCode contains carConstantHashCodeAA"); + } else { + System.out.println("carsConstantHashCode doesn't contain carConstantHashCodeAA"); + } + + if (carConstantHashCodeA.equals(carConstantHashCodeAA)) { + System.out.println("carConstantHashCodeA == carConstantHashCodeAA"); + } else { + System.out.println("carConstantHashCodeA != carConstantHashCodeAA"); + } + + System.out.println("carsConstantHashCode size before add carConstantHashCodeAA"); + System.out.println("carsConstantHashCode size: " + carsConstantHashCode.size()); + + carsConstantHashCode.add(carConstantHashCodeAA); + + System.out.println("carsConstantHashCode size after add carConstantHashCodeAA"); + System.out.println("carsConstantHashCode size: " + carsConstantHashCode.size()); + + + + /** + * without equals: + * + * HashSet doesn't work + * + */ + CarWithoutEquals carWithoutEqualsA = new CarWithoutEquals(1, "carWithoutEqualsA"); + CarWithoutEquals carWithoutEqualsB = new CarWithoutEquals(2, "carWithoutEqualsB"); + CarWithoutEquals carWithoutEqualsC = new CarWithoutEquals(3, "carWithoutEqualsC"); + CarWithoutEquals carWithoutEqualsAA = new CarWithoutEquals(1, "carWithoutEqualsA"); + + HashSet carsWithoutEquals = new HashSet<>(); + + carsWithoutEquals.add(null); + carsWithoutEquals.add(carWithoutEqualsA); + carsWithoutEquals.add(carWithoutEqualsB); + + if (carsWithoutEquals.contains(carWithoutEqualsC)) { + System.out.println("carsWithoutEquals contains carWithoutEqualsC"); + } else { + System.out.println("carsWithoutEquals doesn't contain carWithoutEqualsC"); + } + + if (carsWithoutEquals.contains(carWithoutEqualsAA)) { + System.out.println("carsWithoutEquals contains carWithoutEqualsAA"); + } else { + System.out.println("carsWithoutEquals doesn't contain carWithoutEqualsAA"); + } + + if (carWithoutEqualsA.equals(carWithoutEqualsAA)) { + System.out.println("carWithoutEqualsA == carWithoutEqualsAA"); + } else { + System.out.println("carWithoutEqualsA != carWithoutEqualsAA"); + } + + System.out.println("carsWithoutEquals size before add carWithoutEqualsAA"); + System.out.println("carsWithoutEquals size: " + carsWithoutEquals.size()); + + carsWithoutEquals.add(carWithoutEqualsAA); + + System.out.println("carsWithoutEquals size after add carWithoutEqualsAA"); + System.out.println("carsWithoutEquals size: " + carsWithoutEquals.size()); + } +} -- 2.1.4