equals-hashcode: what is it for (HashSet relation)
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 4 Oct 2015 20:19:54 +0000 (22:19 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 4 Oct 2015 20:19:54 +0000 (22:19 +0200)
Allgemeines/Equals-HashCode/src/de/example/equalshash/Car.java [new file with mode: 0644]
Allgemeines/Equals-HashCode/src/de/example/equalshash/CarConstantHashCode.java [new file with mode: 0644]
Allgemeines/Equals-HashCode/src/de/example/equalshash/CarWithoutEquals.java [new file with mode: 0644]
Allgemeines/Equals-HashCode/src/de/example/equalshash/CarWithoutHashCode.java [new file with mode: 0644]
Allgemeines/Equals-HashCode/src/de/example/equalshash/EqualsHashMainTest.java [new file with mode: 0644]

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 (file)
index 0000000..579da54
--- /dev/null
@@ -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 (file)
index 0000000..2f5680a
--- /dev/null
@@ -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 (file)
index 0000000..c4c0866
--- /dev/null
@@ -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 (file)
index 0000000..256247e
--- /dev/null
@@ -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 (file)
index 0000000..cd7bf3d
--- /dev/null
@@ -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<Car> 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<CarWithoutHashCode> 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<CarConstantHashCode> 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<CarWithoutEquals> 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());
+       }
+}