Skip to content

Instantly share code, notes, and snippets.

@LeoHeo
Last active June 10, 2018 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeoHeo/a1327a68a5eb2a70346ddfb9bdd0a3c5 to your computer and use it in GitHub Desktop.
Save LeoHeo/a1327a68a5eb2a70346ddfb9bdd0a3c5 to your computer and use it in GitHub Desktop.
@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`를 사용해봤다.
}
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