Skip to content

Instantly share code, notes, and snippets.

@andreikastsiuk
Created July 12, 2019 11:18
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 andreikastsiuk/f5ee9f64f560490673f1a487627f4717 to your computer and use it in GitHub Desktop.
Save andreikastsiuk/f5ee9f64f560490673f1a487627f4717 to your computer and use it in GitHub Desktop.
Simple sample of overrided equals and hashcode methods in java
@Override
public boolean equals(@Nullable Object o) {
if (o == null) {
return false;
}
if (o == this) {
return true;
}
if (!(o instanceof MyObject)) {
return false;
}
MyObject other = (MyObject) o;
return url != null && url.equals(other.url)
&& size == other.size;
}
@Override
public int hashCode() {
int hash = 7;
hash = 31 * hash + size;
hash = 31 * hash + (url == null ? 0 : url.hashCode());
return hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment