Skip to content

Commit 456f4b0

Browse files
committed
BAEL-2388: examples for equals and hashCode
1 parent ba98695 commit 456f4b0

File tree

9 files changed

+239
-0
lines changed

9 files changed

+239
-0
lines changed

java-collections-maps/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
<artifactId>streamex</artifactId>
4242
<version>0.6.5</version>
4343
</dependency>
44+
<dependency>
45+
<groupId>nl.jqno.equalsverifier</groupId>
46+
<artifactId>equalsverifier</artifactId>
47+
<version>3.0.3</version>
48+
<scope>test</scope>
49+
</dependency>
4450
</dependencies>
4551

4652
<properties>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.map.hashcode;
2+
3+
class Money {
4+
5+
int amount;
6+
String currencyCode;
7+
8+
Money(int amount, String currencyCode) {
9+
this.amount = amount;
10+
this.currencyCode = currencyCode;
11+
}
12+
13+
@Override
14+
public boolean equals(Object o) {
15+
if (o == this)
16+
return true;
17+
if (!(o instanceof Money))
18+
return false;
19+
Money other = (Money)o;
20+
return this.amount == other.amount
21+
&& this.currencyCode == other.currencyCode;
22+
}
23+
24+
@Override
25+
public int hashCode() {
26+
int result = 17;
27+
result = 31 * result + amount;
28+
result = 31 * result + currencyCode.hashCode();
29+
return result;
30+
}
31+
32+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.map.hashcode;
2+
3+
class Team {
4+
5+
final String city;
6+
final String department;
7+
8+
Team(String city, String department) {
9+
this.city = city;
10+
this.department = department;
11+
}
12+
13+
@Override
14+
public final boolean equals(Object o) {
15+
if (o == this)
16+
return true;
17+
if (!(o instanceof Team))
18+
return false;
19+
Team otherTeam = (Team)o;
20+
boolean cityEquals = (this.city == null && otherTeam.city == null)
21+
|| this.city != null && this.city.equals(otherTeam.city);
22+
boolean departmentEquals = (this.department == null && otherTeam.department == null)
23+
|| this.department != null && this.department.equals(otherTeam.department);
24+
return cityEquals && departmentEquals;
25+
}
26+
27+
@Override
28+
public final int hashCode() {
29+
int result = 17;
30+
if (city != null) {
31+
result = 31 * result + city.hashCode();
32+
}
33+
if (department != null) {
34+
result = 31 * result + department.hashCode();
35+
}
36+
return result;
37+
}
38+
39+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.map.hashcode;
2+
3+
class Voucher {
4+
5+
private Money value;
6+
private String store;
7+
8+
Voucher(int amount, String currencyCode, String store) {
9+
this.value = new Money(amount, currencyCode);
10+
this.store = store;
11+
}
12+
13+
@Override
14+
public boolean equals(Object o) {
15+
if (o == this)
16+
return true;
17+
if (!(o instanceof Voucher))
18+
return false;
19+
Voucher other = (Voucher)o;
20+
return this.value == other.value
21+
&& this.store == other.store;
22+
}
23+
24+
@Override
25+
public int hashCode() {
26+
int result = 17;
27+
result = 31 * result + value.hashCode();
28+
result = 31 * result + store.hashCode();
29+
return result;
30+
}
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.map.hashcode;
2+
3+
class WrongTeam {
4+
5+
String city;
6+
String department;
7+
8+
WrongTeam(String city, String department) {
9+
this.city = city;
10+
this.department = department;
11+
}
12+
13+
@Override
14+
public boolean equals(Object o) {
15+
if (o == this)
16+
return true;
17+
if (!(o instanceof WrongTeam))
18+
return false;
19+
WrongTeam otherTeam = (WrongTeam)o;
20+
return this.city == otherTeam.city
21+
&& this.department == otherTeam.department;
22+
}
23+
24+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.map.hashcode;
2+
3+
class WrongVoucher extends Money {
4+
5+
private String store;
6+
7+
WrongVoucher(int amount, String currencyCode, String store) {
8+
super(amount, currencyCode);
9+
10+
this.store = store;
11+
}
12+
13+
@Override
14+
public boolean equals(Object o) {
15+
if (o == this)
16+
return true;
17+
if (!(o instanceof WrongVoucher))
18+
return false;
19+
WrongVoucher other = (WrongVoucher)o;
20+
return this.amount == other.amount
21+
&& this.currencyCode == other.currencyCode
22+
&& this.store == other.store;
23+
}
24+
25+
@Override
26+
public int hashCode() {
27+
int result = 17;
28+
result = 31 * result + amount;
29+
result = 31 * result + currencyCode.hashCode();
30+
result = 31 * result + store.hashCode();
31+
return result;
32+
}
33+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Non-nullity: hashCode throws NullPointerException on field city.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.map.hashcode;
2+
3+
import static org.junit.Assert.assertTrue;
4+
import static org.junit.Assert.assertFalse;
5+
6+
import org.junit.Test;
7+
8+
public class MoneyUnitTest {
9+
10+
@Test
11+
public void givenMoneyInstancesWithSameAmountAndCurrency_whenEquals_thenReturnsTrue() {
12+
Money income = new Money(55, "USD");
13+
Money expenses = new Money(55, "USD");
14+
15+
assertTrue(income.equals(expenses));
16+
}
17+
18+
@Test
19+
public void givenMoneyAndVoucherInstances_whenEquals_thenReturnValuesArentSymmetric() {
20+
Money cash = new Money(42, "USD");
21+
WrongVoucher voucher = new WrongVoucher(42, "USD", "Amazon");
22+
23+
assertFalse(voucher.equals(cash));
24+
assertTrue(cash.equals(voucher));
25+
}
26+
27+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.map.hashcode;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
import org.junit.Test;
10+
11+
import nl.jqno.equalsverifier.EqualsVerifier;
12+
13+
public class TeamUnitTest {
14+
15+
@Test
16+
public void givenMapKeyWithHashCode_whenSearched_thenReturnsCorrectValue() {
17+
Map<Team,String> leaders = new HashMap<>();
18+
leaders.put(new Team("New York", "development"), "Anne");
19+
leaders.put(new Team("Boston", "development"), "Brian");
20+
leaders.put(new Team("Boston", "marketing"), "Charlie");
21+
22+
Team myTeam = new Team("New York", "development");
23+
String myTeamleader = leaders.get(myTeam);
24+
25+
assertEquals("Anne", myTeamleader);
26+
}
27+
28+
@Test
29+
public void givenMapKeyWithoutHashCode_whenSearched_thenReturnsWrongValue() {
30+
Map<WrongTeam,String> leaders = new HashMap<>();
31+
leaders.put(new WrongTeam("New York", "development"), "Anne");
32+
leaders.put(new WrongTeam("Boston", "development"), "Brian");
33+
leaders.put(new WrongTeam("Boston", "marketing"), "Charlie");
34+
35+
WrongTeam myTeam = new WrongTeam("New York", "development");
36+
String myTeamleader = leaders.get(myTeam);
37+
38+
assertFalse("Anne".equals(myTeamleader));
39+
}
40+
41+
@Test
42+
public void equalsContract() {
43+
EqualsVerifier.forClass(Team.class).verify();
44+
}
45+
46+
}

0 commit comments

Comments
 (0)