Skip to content

Commit 69b8522

Browse files
authored
BAEL-4925 (eugenp#10746)
* BAEL-4925 * BAEL-4925
1 parent 92d4fd0 commit 69b8522

3 files changed

Lines changed: 191 additions & 0 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.baeldung.hash;
2+
3+
import java.util.Objects;
4+
5+
public class Player {
6+
private String firstName;
7+
private String lastName;
8+
private String position;
9+
10+
public Player() {
11+
12+
}
13+
14+
public Player(String firstName, String lastName, String position) {
15+
this.firstName = firstName;
16+
this.lastName = lastName;
17+
this.position = position;
18+
}
19+
20+
public String getFirstName() {
21+
return firstName;
22+
}
23+
24+
public void setFirstName(String firstName) {
25+
this.firstName = firstName;
26+
}
27+
28+
public String getLastName() {
29+
return lastName;
30+
}
31+
32+
public void setLastName(String lastName) {
33+
this.lastName = lastName;
34+
}
35+
36+
public String getPosition() {
37+
return position;
38+
}
39+
40+
public void setPosition(String position) {
41+
this.position = position;
42+
}
43+
44+
@Override
45+
public int hashCode() {
46+
return Objects.hash(firstName, lastName, position);
47+
}
48+
49+
@Override
50+
public boolean equals(Object obj) {
51+
if (this == obj) {
52+
return true;
53+
}
54+
55+
if (obj == null) {
56+
return false;
57+
}
58+
59+
if (getClass() != obj.getClass()) {
60+
return false;
61+
}
62+
63+
Player other = (Player) obj;
64+
65+
if (firstName == null) {
66+
if (other.firstName != null) {
67+
return false;
68+
}
69+
} else if (!firstName.equals(other.firstName)) {
70+
return false;
71+
}
72+
73+
if (lastName == null) {
74+
if (other.lastName != null) {
75+
return false;
76+
}
77+
} else if (!lastName.equals(other.lastName)) {
78+
return false;
79+
}
80+
81+
if (position == null) {
82+
if (other.position != null) {
83+
return false;
84+
}
85+
} else if (!position.equals(other.position)) {
86+
return false;
87+
}
88+
return true;
89+
}
90+
91+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung.hash;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotEquals;
5+
6+
import java.util.Objects;
7+
8+
import org.junit.Test;
9+
10+
public class HashCodeUnitTest {
11+
12+
@Test
13+
public void whenCallingObjectsHashCodeOnIndenticalObjects_thenSameHashCodeReturned() {
14+
String stringOne = "test";
15+
String stringTwo = "test";
16+
int hashCode1 = Objects.hashCode(stringOne);
17+
int hashCode2 = Objects.hashCode(stringTwo);
18+
19+
assertEquals(hashCode1, hashCode2);
20+
}
21+
22+
@Test
23+
public void whenCallingObjectsHashCodeOnNullObject_thenZeroReturned() {
24+
String nullString = null;
25+
int hashCode = Objects.hashCode(nullString);
26+
assertEquals(0, hashCode);
27+
}
28+
29+
@Test
30+
public void whenCallingObjectHashCodeOnIndenticalObjects_thenSameHashCodeReturned() {
31+
Double valueOne = Double.valueOf(1.0012);
32+
Double valueTwo = Double.valueOf(1.0012);
33+
34+
int hashCode1 = valueOne.hashCode();
35+
int hashCode2 = valueTwo.hashCode();
36+
37+
assertEquals(hashCode1, hashCode2);
38+
}
39+
40+
@Test(expected = NullPointerException.class)
41+
public void whenCallingObjectHashCodeOnNullObject_theNullPointerExceptionThrown() {
42+
Double value = null;
43+
value.hashCode();
44+
}
45+
46+
@Test
47+
public void whenCallingObjectsHashOnStrings_thenSameHashCodeReturned() {
48+
String strOne = "one";
49+
String strTwo = "two";
50+
String strOne2 = "one";
51+
String strTwo2 = "two";
52+
53+
int hashCode1 = Objects.hash(strOne, strTwo);
54+
int hashCode2 = Objects.hash(strOne2, strTwo2);
55+
56+
assertEquals(hashCode1, hashCode2);
57+
}
58+
59+
@Test
60+
public void whenCallingObjectsHashOnSingleString_thenDifferentHashcodeFromObjectsHashCodeCallReturned() {
61+
String testString = "test string";
62+
int hashCode1 = Objects.hash(testString);
63+
int hashCode2 = Objects.hashCode(testString);
64+
65+
assertNotEquals(hashCode1, hashCode2);
66+
}
67+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.hash;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.Arrays;
6+
7+
import org.junit.Test;
8+
9+
public class PlayerUnitTest {
10+
11+
@Test
12+
public void whenCallingHashCodeOnIdenticalValue_thenSameHashCodeReturned() {
13+
Player player = new Player("Eduardo", "Rodriguez", "Pitcher");
14+
Player indenticalPlayer = new Player("Eduardo", "Rodriguez", "Pitcher");
15+
16+
int hashCode1 = player.hashCode();
17+
int hashCode2 = player.hashCode();
18+
int hashCode3 = indenticalPlayer.hashCode();
19+
20+
assertEquals(hashCode1, hashCode2);
21+
assertEquals(hashCode1, hashCode3);
22+
}
23+
24+
@Test
25+
public void whenCallingHashCodeAndArraysHashCode_thenSameHashCodeReturned() {
26+
Player player = new Player("Bobby", "Dalbec", "First Base");
27+
int hashcode1 = player.hashCode();
28+
String[] playerInfo = { "Bobby", "Dalbec", "First Base" };
29+
int hashcode2 = Arrays.hashCode(playerInfo);
30+
31+
assertEquals(hashcode1, hashcode2);
32+
}
33+
}

0 commit comments

Comments
 (0)