forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
deb79bb
commit 752086e
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
core-java/src/main/java/com/baeldung/hashcode/application/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.baeldung.application; | ||
|
||
import com.baeldung.entities.User; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
Map<User, User> users = new HashMap<>(); | ||
User user1 = new User(1L, "John", "[email protected]"); | ||
User user2 = new User(2L, "Jennifer", "[email protected]"); | ||
User user3 = new User(3L, "Mary", "[email protected]"); | ||
|
||
users.put(user1, user1); | ||
users.put(user2, user2); | ||
users.put(user3, user3); | ||
|
||
if (users.containsKey(user1)) { | ||
System.out.print("User found in the collection"); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
core-java/src/main/java/com/baeldung/hashcode/entities/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
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; | ||
|
||
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)); | ||
} | ||
|
||
@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 | ||
} |
30 changes: 30 additions & 0 deletions
30
core-java/src/test/java/com/baeldung/hashcode/application/ApplicationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
core-java/src/test/java/com/baeldung/hashcode/entities/UserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", "[email protected]"); | ||
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()); | ||
} | ||
} |