Skip to content

Commit 34dc899

Browse files
Alejandro Gervasiozhendrikse
authored andcommitted
Custom Efficient hashCode() Implementations - Alejandro Gervasio - alejandro.gervasio@gmail.com (eugenp#2301)
* Initial Commit * Updated pom.xml * Updated User class * Updated pom.xml * Updated Application class
1 parent 1be49eb commit 34dc899

5 files changed

Lines changed: 164 additions & 0 deletions

File tree

‎core-java/hashcode/pom.xml‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.baeldung.hashcode</groupId>
7+
<artifactId>hashcode</artifactId>
8+
<version>1.0</version>
9+
<build>
10+
<plugins>
11+
<plugin>
12+
<groupId>org.apache.maven.plugins</groupId>
13+
<artifactId>maven-compiler-plugin</artifactId>
14+
<configuration>
15+
<source>1.8</source>
16+
<target>1.8</target>
17+
</configuration>
18+
</plugin>
19+
</plugins>
20+
</build>
21+
<dependencies>
22+
<dependency>
23+
<groupId>junit</groupId>
24+
<artifactId>junit</artifactId>
25+
<version>4.12</version>
26+
<scope>test</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.slf4j</groupId>
30+
<artifactId>slf4j-api</artifactId>
31+
<version>1.7.25</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.slf4j</groupId>
35+
<artifactId>slf4j-simple</artifactId>
36+
<version>1.7.25</version>
37+
</dependency>
38+
</dependencies>
39+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.application;
2+
3+
import com.baeldung.entities.User;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
Map<User, User> users = new HashMap<>();
11+
User user1 = new User(1L, "John", "john@domain.com");
12+
User user2 = new User(2L, "Jennifer", "jennifer@domain.com");
13+
User user3 = new User(3L, "Mary", "mary@domain.com");
14+
15+
users.put(user1, user1);
16+
users.put(user2, user2);
17+
users.put(user3, user3);
18+
19+
if (users.containsKey(user1)) {
20+
System.out.print("User found in the collection");
21+
}
22+
}
23+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.entities;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class User {
7+
8+
private final Logger logger = LoggerFactory.getLogger(User.class);
9+
private long id;
10+
private String name;
11+
private String email;
12+
13+
public User(long id, String name, String email) {
14+
this.id = id;
15+
this.name = name;
16+
this.email = email;
17+
}
18+
19+
@Override
20+
public boolean equals(Object o) {
21+
if (this == o) return true;
22+
if (o == null) return false;
23+
if (this.getClass() != o.getClass()) return false;
24+
User user = (User) o;
25+
return id != user.id && (!name.equals(user.name) && !email.equals(user.email));
26+
}
27+
28+
@Override
29+
public int hashCode() {
30+
int hash = 7;
31+
hash = 31 * hash + (int) id;
32+
hash = 31 * hash + (name == null ? 0 : name.hashCode());
33+
hash = 31 * hash + (email == null ? 0 : email.hashCode());
34+
logger.info("hashCode() method called - Computed hash: " + hash);
35+
return hash;
36+
}
37+
// getters and setters here
38+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.application;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.PrintStream;
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class ApplicationTest {
11+
12+
private ByteArrayOutputStream outContent;
13+
14+
@Before
15+
public void setUpPrintStreamInstance() throws Exception {
16+
this.outContent = new ByteArrayOutputStream();
17+
System.setOut(new PrintStream(outContent));
18+
}
19+
20+
@After
21+
public void tearDownByteArrayOutputStream() throws Exception {
22+
outContent = null;
23+
}
24+
25+
@Test
26+
public void main_NoInputState_TextPrintedToConsole() throws Exception {
27+
Application.main(new String[]{});
28+
assertEquals("User found in the collection", outContent.toString());
29+
}
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.entities;
2+
3+
import org.junit.After;
4+
import org.junit.Assert;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
public class UserTest {
9+
10+
private User user;
11+
private User comparisonUser;
12+
13+
@Before
14+
public void setUpUserInstances() {
15+
this.user = new User(1L, "test", "test@domain.com");
16+
this.comparisonUser = this.user;
17+
}
18+
19+
@After
20+
public void tearDownUserInstances() {
21+
user = null;
22+
comparisonUser = null;
23+
}
24+
25+
@Test
26+
public void equals_EqualUserInstance_TrueAssertion(){
27+
Assert.assertTrue(user.equals(comparisonUser));
28+
}
29+
30+
@Test
31+
public void hashCode_UserHash_TrueAssertion() {
32+
Assert.assertEquals(1792276941, user.hashCode());
33+
}
34+
}

0 commit comments

Comments
 (0)