Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


How to override equals() and hashCode() method in java?



How to override equals() and hashCode() method in java?..

Answer / javamasque

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}

Employee emp = (Employee) obj;
return id == emp.id
&& (firstName == emp.firstName
|| (firstName != null && firstName.equals(emp.getFirstName())))
&& (lastName == emp.lastName || (lastName != null && lastName .equals(emp.getLastName())));

}// equals method ends

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((firstName == null) ? 0 :frstName.hashCode());
result = prime * result + id;
result = prime * result
+ ((lastName == null) ? 0 : lastName.hashCode());
return result;

}// hashCode method ends

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

Differentiate between == and equals().

0 Answers  


Explain about transient variables in java?

0 Answers  


Can you override static method in java?

1 Answers  


What does nullpointerexception mean?

0 Answers  


Name some OOPS Concepts in Java?

0 Answers  


What is void class in java?

0 Answers  


What is treeset in java?

0 Answers  


How is hashcode calculated in java?

0 Answers  


what is finalmethod & final variable with example?

6 Answers   HP,


Write java program to reverse string without using api?

0 Answers  


How to make a method thread safe without using synchronized keyword?

8 Answers   Persistent, Societe Generale,


what do you mean by classloader in java?

0 Answers  


Categories