Last active
June 10, 2018 04:12
-
-
Save LeoHeo/a1327a68a5eb2a70346ddfb9bdd0a3c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public boolean equals(Object o) { | |
if (this == o) { | |
return true; | |
} | |
if (!(o instanceof Student)) { | |
return false; | |
} | |
Student student = (Student) o; | |
return Objects.equals(getId(), student.getId()); // Java7+ 부터 사용할 수 있는 `Objects.equals`를 사용해봤다. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.leoheo.equals; | |
public class Main { | |
public static void main(String[] args) { | |
Student leo1 = new Student(1L, "Leo"); | |
Student leo2 = new Student(1L, "Leo"); | |
System.out.println("leo1 hashcode => " + leo1.hashCode()); // 1625635731 | |
System.out.println("leo2 hashcode => " + leo2.hashCode()); // 1580066828 | |
System.out.println("leo1.equals(leo2) => " + leo1.equals(leo2)); // true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment