Created
July 12, 2019 11:18
-
-
Save andreikastsiuk/f5ee9f64f560490673f1a487627f4717 to your computer and use it in GitHub Desktop.
Simple sample of overrided equals and hashcode methods in java
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(@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