Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals() to check equality of two objects

 
vijay kumarg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an Employee object like
Employee emp=new Employee(String,int)
How to check equality of two Employee objects.
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
depends on how you have modeled the employee object. For instance you assume the employee no would uniquely identify an employee,
then you can compare employee id of the two objects to see if they are same.
 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use equals() method
 
vijay kumarg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ajay,
Each employee should have unique ename i.e. String and unique ecode i.e. int.
Here is the Employee class:

public class Employee {
int ecode;
String ename;
public Employee(String ename,int ecode)
{
this.ename=ename;
this.ecode=ecode;
}
public String toString()
{
return "Employee name is >>"+this.ename+"Employee code is>> "+this.ecode;
}
public boolean equals(final Object obj)
{
// code to test equality
}
}

what should I write in equals()?
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gurudhaasan:
use equals() method

Gurudhaasan, you are (as in the "programmer,developr" post) incorrect. The
equals method tests if two objects are the same.. but in the case with
employees they are the same if they have the same value. Please consider
your postings.. and remember to state if your not 100% sure that your
answer is correct.



vijayk gopu: Each employee should have unique ename i.e. String and unique ecode
vijayk gopu: what should I write in equals()?


You have actually answered your own question if a unique employee id
identified by a a unique String and a unique integer, how would you
implement an equalily check, i.e. when two objects are identical (have the
same values) ?

Happy coding

/Svend Rost, who'll check back later
 
Shashi Kant
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through following link.
Hope that will help you.

Java World
 
vijay kumarg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Svend,

Thanks for the reply.

if a unique employee id
identified by a a unique String and a unique integer, how would you
implement an equalily check, i.e. when two objects are identical (have the
same values) ?


I am writing a code in JUnit where I need to check for equality of an existing object say Employee("aa",1111) with some external object with same ename and ecode i.e. "aa" and 1111 respectively.
How to acomplish this?

So If I have to call obj.equals(existing Employe object in an Hashtable)
what should I write in equals() method?
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vijayk gopu:

I am writing a code in JUnit where I need to check for equality of an existing object say Employee("aa",1111) with some external object with same ename and ecode i.e. "aa" and 1111 respectively.
How to acomplish this?

So If I have to call obj.equals(existing Employe object in an Hashtable)
what should I write in equals() method?


Okay, two objects (employees) are equal if they have/are in the same state,
i.e. their variables (ename and ecode) have identical values.

Let's look at it shall we...


Does this help? try to implement it.. if you run into problems, tell me
what the problem is and how you could imagine you should solve it..

/Svend Rost, who'll check back later
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your intent is to override the equals() method in Object class the method signiture must be:


Note that if you override equals() then you should override hashCode(). Look for examples of well written equals() and hashCode() here at javaranch.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic