Write a class and override the equals and hashcode method ?
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Interview Questions and Answers

 Q1. Write a class and override the equals and hashcode method ?Core Java
Ans. class Student
{
///
///
///
private int id;
private String name;

///
///
///
public Student(int id, String name)
{
this.name = name;
this.id = id;

}// Student

///
///
///
public int getId()
{
return id;

}// getId

///
///
///
public void setId(int id)
{
this.id = id;

}// setId

///
///
///
public String getName()
{
return name;

}// getName

///
///
///
public void setName(String name)
{
this.name = name;

}// setName

@Override
public boolean equals(Object obj)
{
if (obj == null) return false;
if (!(obj instanceof Student))
return false;
if (obj == this)
return true;

return this.getId() == ((Student) obj).getId();

}// equals

///
///
///
@Override
public int hashCode()
{
return this.getId();

}// hashCode

}// Student

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve          Asked in 1 Companies


Related Questions

  Which String class does not override the equals() and hashCode() methods, inheriting them directly from class Object?
 How many threads can access a concurrent hashmap when the hashcode is overridden and always returns a zero.



Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: