Created
October 24, 2024 09:39
-
-
Save karthiks/30ad8fcf1021a92b4d3bca31de269a7d to your computer and use it in GitHub Desktop.
Person and Address class with equal() and hashCode() generated using Eclipse IDE without using Objects.
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() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result + ((id == null) ? 0 : id.hashCode()); | |
result = prime * result + ((name == null) ? 0 : name.hashCode()); | |
return result; | |
} | |
@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; | |
if (id == null) { | |
if (other.id != null) | |
return false; | |
} else if (!id.equals(other.id)) | |
return false; | |
if (name == null) { | |
if (other.name != null) | |
return false; | |
} else if (!name.equals(other.name)) | |
return false; | |
return true; | |
} | |
} | |
class Address { | |
private String road; | |
private String city; | |
private String pincode; | |
@Override | |
public int hashCode() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result + ((city == null) ? 0 : city.hashCode()); | |
result = prime * result + ((pincode == null) ? 0 : pincode.hashCode()); | |
result = prime * result + ((road == null) ? 0 : road.hashCode()); | |
return result; | |
} | |
@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; | |
if (city == null) { | |
if (other.city != null) | |
return false; | |
} else if (!city.equals(other.city)) | |
return false; | |
if (pincode == null) { | |
if (other.pincode != null) | |
return false; | |
} else if (!pincode.equals(other.pincode)) | |
return false; | |
if (road == null) { | |
if (other.road != null) | |
return false; | |
} else if (!road.equals(other.road)) | |
return false; | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment