Created
October 24, 2024 09:44
-
-
Save karthiks/9290d9d94e76506663c124fcf5673162 to your computer and use it in GitHub Desktop.
Person and Address class with equal() and hashCode() generated using Eclipse IDE but using java 1.7 Objects's static methods.
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
public class Person { | |
private Long id; | |
private String name; | |
@Override | |
public int hashCode() { | |
return Objects.hash(id, name); | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) | |
return true; | |
if (obj == null) | |
return false; | |
if (getClass() != obj.getClass()) | |
return false; | |
Person other = (Person) obj; | |
return Objects.equals(id, other.id) && Objects.equals(name, other.name); | |
} | |
} | |
class Address { | |
private String road; | |
private String city; | |
private String pincode; | |
@Override | |
public int hashCode() { | |
return Objects.hash(city, pincode, road); | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) | |
return true; | |
if (obj == null) | |
return false; | |
if (getClass() != obj.getClass()) | |
return false; | |
Address other = (Address) obj; | |
return Objects.equals(city, other.city) && Objects.equals(pincode, other.pincode) | |
&& Objects.equals(road, other.road); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment