Skip to content

Commit

Permalink
Add hashcode to pair
Browse files Browse the repository at this point in the history
  • Loading branch information
chiquitinxx committed Dec 7, 2023
1 parent 2c93f10 commit e67f441
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/dev/yila/functional/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public boolean equals(Object obj) {
Objects.equals(this.right, ((Pair<?, ?>) obj).right);
}

@Override
public int hashCode() {
return this.left.hashCode() + this.right.hashCode();
}

@Override
public String toString() {
return "Pair(" + this.left + ", " + this.right + ")";
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/dev/yila/functional/PairTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ void equalPair() {
assertEquals(hello5, sameHello5);
}

@Test
void hashcode() {
Pair<String, Integer> pair = new Pair<>("hello", 5);

assertEquals(pair.hashCode(), "hello".hashCode() + Integer.valueOf(5).hashCode());
}

@Test
void pairToString() {
Pair<String, Integer> hello5 = new Pair<>("hello", 5);
Expand Down

0 comments on commit e67f441

Please sign in to comment.