From 1d4ac342d43c9dca259e9cc349ca86968f6f99a2 Mon Sep 17 00:00:00 2001 From: Alejandro Gervasio Date: Thu, 20 Jul 2017 19:50:13 -0300 Subject: [PATCH 1/5] Initial Commit --- core-java/hashcode/pom.xml | 29 ++++++++++++++++ .../com/baeldung/application/Application.java | 22 ++++++++++++ .../main/java/com/baeldung/entities/User.java | 32 +++++++++++++++++ .../baeldung/application/ApplicationTest.java | 30 ++++++++++++++++ .../java/com/baeldung/entities/UserTest.java | 34 +++++++++++++++++++ 5 files changed, 147 insertions(+) create mode 100644 core-java/hashcode/pom.xml create mode 100644 core-java/hashcode/src/main/java/com/baeldung/application/Application.java create mode 100644 core-java/hashcode/src/main/java/com/baeldung/entities/User.java create mode 100644 core-java/hashcode/src/test/java/com/baeldung/application/ApplicationTest.java create mode 100644 core-java/hashcode/src/test/java/com/baeldung/entities/UserTest.java diff --git a/core-java/hashcode/pom.xml b/core-java/hashcode/pom.xml new file mode 100644 index 000000000000..12e575443b5d --- /dev/null +++ b/core-java/hashcode/pom.xml @@ -0,0 +1,29 @@ + + + 4.0.0 + com.baeldung.hashcode + hashcode + 1.0 + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.7 + 1.7 + + + + + + + junit + junit + 4.12 + test + + + \ No newline at end of file diff --git a/core-java/hashcode/src/main/java/com/baeldung/application/Application.java b/core-java/hashcode/src/main/java/com/baeldung/application/Application.java new file mode 100644 index 000000000000..13d97fec5770 --- /dev/null +++ b/core-java/hashcode/src/main/java/com/baeldung/application/Application.java @@ -0,0 +1,22 @@ +package com.baeldung.application; + +import com.baeldung.entities.User; +import java.util.*; + +public class Application { + + public static void main(String[] args) { + Map users = new HashMap<>(); + User user1 = new User(1L, "John", "john@domain.com"); + User user2 = new User(2L, "Jennifer", "jennifer@domain.com"); + User user3 = new User(3L, "Mary", "mary@domain.com"); + + users.put(user1, user1); + users.put(user2, user2); + users.put(user3, user3); + + if (users.containsKey(user1)) { + System.out.print("User found in the collection"); + } + } +} diff --git a/core-java/hashcode/src/main/java/com/baeldung/entities/User.java b/core-java/hashcode/src/main/java/com/baeldung/entities/User.java new file mode 100644 index 000000000000..ae41de0cfcea --- /dev/null +++ b/core-java/hashcode/src/main/java/com/baeldung/entities/User.java @@ -0,0 +1,32 @@ +package com.baeldung.entities; + +public class User { + + private long id; + private String name; + private String email; + + public User(long id, String name, String email) { + this.id = id; + this.name = name; + this.email = email; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null) return false; + if (this.getClass() != o.getClass()) return false; + User user = (User) o; + return id != user.id && (!name.equals(user.name) && !email.equals(user.email)); + } + + public int hashCode() { + int hash = 7; + hash = 31 * hash + (int) id; + hash = 31 * hash + (name == null ? 0 : name.hashCode()); + hash = 31 * hash + (email == null ? 0 : email.hashCode()); + return hash; + } + // getters and setters here +} diff --git a/core-java/hashcode/src/test/java/com/baeldung/application/ApplicationTest.java b/core-java/hashcode/src/test/java/com/baeldung/application/ApplicationTest.java new file mode 100644 index 000000000000..dcd853f451d9 --- /dev/null +++ b/core-java/hashcode/src/test/java/com/baeldung/application/ApplicationTest.java @@ -0,0 +1,30 @@ +package com.baeldung.application; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import static org.junit.Assert.assertEquals; + +public class ApplicationTest { + + private ByteArrayOutputStream outContent; + + @Before + public void setUpPrintStreamInstance() throws Exception { + this.outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + } + + @After + public void tearDownByteArrayOutputStream() throws Exception { + outContent = null; + } + + @Test + public void main_NoInputState_TextPrintedToConsole() throws Exception { + Application.main(new String[]{}); + assertEquals("User found in the collection", outContent.toString()); + } +} \ No newline at end of file diff --git a/core-java/hashcode/src/test/java/com/baeldung/entities/UserTest.java b/core-java/hashcode/src/test/java/com/baeldung/entities/UserTest.java new file mode 100644 index 000000000000..01f6085d7eac --- /dev/null +++ b/core-java/hashcode/src/test/java/com/baeldung/entities/UserTest.java @@ -0,0 +1,34 @@ +package com.baeldung.entities; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class UserTest { + + private User user; + private User comparisonUser; + + @Before + public void setUpUserInstances() { + this.user = new User(1L, "test", "test@domain.com"); + this.comparisonUser = this.user; + } + + @After + public void tearDownUserInstances() { + user = null; + comparisonUser = null; + } + + @Test + public void equals_EqualUserInstance_TrueAssertion(){ + Assert.assertTrue(user.equals(comparisonUser)); + } + + @Test + public void hashCode_UserHash_TrueAssertion() { + Assert.assertEquals(1792276941, user.hashCode()); + } +} \ No newline at end of file From 6600ac9b69d1fdef81f6d8ec9ccd0c6da5acf9f5 Mon Sep 17 00:00:00 2001 From: Alejandro Gervasio Date: Thu, 20 Jul 2017 19:57:42 -0300 Subject: [PATCH 2/5] Updated pom.xml --- core-java/hashcode/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core-java/hashcode/pom.xml b/core-java/hashcode/pom.xml index 12e575443b5d..b3d010546613 100644 --- a/core-java/hashcode/pom.xml +++ b/core-java/hashcode/pom.xml @@ -12,8 +12,8 @@ org.apache.maven.plugins maven-compiler-plugin - 1.7 - 1.7 + 1.8 + 1.8 From 827819751422820353b18039754a4080db68342e Mon Sep 17 00:00:00 2001 From: Alejandro Gervasio Date: Thu, 20 Jul 2017 21:09:05 -0300 Subject: [PATCH 3/5] Updated User class --- .../hashcode/src/main/java/com/baeldung/entities/User.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core-java/hashcode/src/main/java/com/baeldung/entities/User.java b/core-java/hashcode/src/main/java/com/baeldung/entities/User.java index ae41de0cfcea..a426b21e7a95 100644 --- a/core-java/hashcode/src/main/java/com/baeldung/entities/User.java +++ b/core-java/hashcode/src/main/java/com/baeldung/entities/User.java @@ -20,7 +20,8 @@ public boolean equals(Object o) { User user = (User) o; return id != user.id && (!name.equals(user.name) && !email.equals(user.email)); } - + + @Override public int hashCode() { int hash = 7; hash = 31 * hash + (int) id; From 2a3acf3cc89d5d1fe0edd5f3d6fe391c50bc64c4 Mon Sep 17 00:00:00 2001 From: Alejandro Gervasio Date: Mon, 24 Jul 2017 15:41:56 -0300 Subject: [PATCH 4/5] Updated pom.xml --- core-java/hashcode/pom.xml | 10 ++++++++++ .../src/main/java/com/baeldung/entities/User.java | 7 ++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/core-java/hashcode/pom.xml b/core-java/hashcode/pom.xml index b3d010546613..393aa69153e9 100644 --- a/core-java/hashcode/pom.xml +++ b/core-java/hashcode/pom.xml @@ -25,5 +25,15 @@ 4.12 test + + org.slf4j + slf4j-api + 1.7.25 + + + org.slf4j + slf4j-simple + 1.7.25 + \ No newline at end of file diff --git a/core-java/hashcode/src/main/java/com/baeldung/entities/User.java b/core-java/hashcode/src/main/java/com/baeldung/entities/User.java index a426b21e7a95..a976233562dc 100644 --- a/core-java/hashcode/src/main/java/com/baeldung/entities/User.java +++ b/core-java/hashcode/src/main/java/com/baeldung/entities/User.java @@ -1,7 +1,11 @@ package com.baeldung.entities; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class User { + private final Logger logger = LoggerFactory.getLogger(User.class); private long id; private String name; private String email; @@ -20,13 +24,14 @@ public boolean equals(Object o) { User user = (User) o; return id != user.id && (!name.equals(user.name) && !email.equals(user.email)); } - + @Override public int hashCode() { int hash = 7; hash = 31 * hash + (int) id; hash = 31 * hash + (name == null ? 0 : name.hashCode()); hash = 31 * hash + (email == null ? 0 : email.hashCode()); + logger.info("hashCode() method called - Computed hash: " + hash); return hash; } // getters and setters here From ff284c697101387c33499fed05a4b858cc8f3511 Mon Sep 17 00:00:00 2001 From: Alejandro Gervasio Date: Mon, 24 Jul 2017 17:51:58 -0300 Subject: [PATCH 5/5] Updated Application class --- .../src/main/java/com/baeldung/application/Application.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core-java/hashcode/src/main/java/com/baeldung/application/Application.java b/core-java/hashcode/src/main/java/com/baeldung/application/Application.java index 13d97fec5770..08c670c82f78 100644 --- a/core-java/hashcode/src/main/java/com/baeldung/application/Application.java +++ b/core-java/hashcode/src/main/java/com/baeldung/application/Application.java @@ -1,7 +1,8 @@ package com.baeldung.application; import com.baeldung.entities.User; -import java.util.*; +import java.util.HashMap; +import java.util.Map; public class Application {