parent
99213ea74c
commit
d75f672e85
|
@ -0,0 +1,91 @@
|
|||
package com.baeldung.hash;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Player {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String position;
|
||||
|
||||
public Player() {
|
||||
|
||||
}
|
||||
|
||||
public Player(String firstName, String lastName, String position) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public void setPosition(String position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(firstName, lastName, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Player other = (Player) obj;
|
||||
|
||||
if (firstName == null) {
|
||||
if (other.firstName != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!firstName.equals(other.firstName)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (lastName == null) {
|
||||
if (other.lastName != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!lastName.equals(other.lastName)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (position == null) {
|
||||
if (other.position != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!position.equals(other.position)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
package com.baeldung.hash;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class HashCodeUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenCallingObjectsHashCodeOnIndenticalObjects_thenSameHashCodeReturned() {
|
||||
String stringOne = "test";
|
||||
String stringTwo = "test";
|
||||
int hashCode1 = Objects.hashCode(stringOne);
|
||||
int hashCode2 = Objects.hashCode(stringTwo);
|
||||
|
||||
assertEquals(hashCode1, hashCode2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingObjectsHashCodeOnNullObject_thenZeroReturned() {
|
||||
String nullString = null;
|
||||
int hashCode = Objects.hashCode(nullString);
|
||||
assertEquals(0, hashCode);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingObjectHashCodeOnIndenticalObjects_thenSameHashCodeReturned() {
|
||||
Double valueOne = Double.valueOf(1.0012);
|
||||
Double valueTwo = Double.valueOf(1.0012);
|
||||
|
||||
int hashCode1 = valueOne.hashCode();
|
||||
int hashCode2 = valueTwo.hashCode();
|
||||
|
||||
assertEquals(hashCode1, hashCode2);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void whenCallingObjectHashCodeOnNullObject_theNullPointerExceptionThrown() {
|
||||
Double value = null;
|
||||
value.hashCode();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingObjectsHashOnStrings_thenSameHashCodeReturned() {
|
||||
String strOne = "one";
|
||||
String strTwo = "two";
|
||||
String strOne2 = "one";
|
||||
String strTwo2 = "two";
|
||||
|
||||
int hashCode1 = Objects.hash(strOne, strTwo);
|
||||
int hashCode2 = Objects.hash(strOne2, strTwo2);
|
||||
|
||||
assertEquals(hashCode1, hashCode2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingObjectsHashOnSingleString_thenDifferentHashcodeFromObjectsHashCodeCallReturned() {
|
||||
String testString = "test string";
|
||||
int hashCode1 = Objects.hash(testString);
|
||||
int hashCode2 = Objects.hashCode(testString);
|
||||
|
||||
assertNotEquals(hashCode1, hashCode2);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.hash;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class PlayerUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenCallingHashCodeOnIdenticalValue_thenSameHashCodeReturned() {
|
||||
Player player = new Player("Eduardo", "Rodriguez", "Pitcher");
|
||||
Player indenticalPlayer = new Player("Eduardo", "Rodriguez", "Pitcher");
|
||||
|
||||
int hashCode1 = player.hashCode();
|
||||
int hashCode2 = player.hashCode();
|
||||
int hashCode3 = indenticalPlayer.hashCode();
|
||||
|
||||
assertEquals(hashCode1, hashCode2);
|
||||
assertEquals(hashCode1, hashCode3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingHashCodeAndArraysHashCode_thenSameHashCodeReturned() {
|
||||
Player player = new Player("Bobby", "Dalbec", "First Base");
|
||||
int hashcode1 = player.hashCode();
|
||||
String[] playerInfo = { "Bobby", "Dalbec", "First Base" };
|
||||
int hashcode2 = Arrays.hashCode(playerInfo);
|
||||
|
||||
assertEquals(hashcode1, hashcode2);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue