0

In java.. If when we have to compare an object with another object. We compare each field in that object.

Student1 object has marks1, marks2, marks3, name, age as fields. Student2 object has marks1, marks2, marks3, name, age as fields. So to check if 2 students are equal or not... we compare each field.

if(Student1.marks1 == Student2.marks1 && 
   Student1.marks2 == Student2.marks2 && 
   Student1.marks3 == Student2.marks3 && 
   Student1.name == Student2.name && 
   Student1.age == Student2.age)
{
    // we say that Students are same 
}

But what if the Student object has many fields.. Student1 object has marks1, marks2, marks3, name, age, address, color, class, country, section, x, y, z like this 100 such fields Student2 object has marks1, marks2, marks3, name, age, address, color, class, country, section, x, y, z like this 100 such fields

So how should we now check whether 2 objects are equal or not..? going with the above approach.. of checking each individual field does not make sense since they are 100 such fields.

Someone was telling this could be done by serialization in java. Can any one please tell how we can go about it or any other way?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Deepak
  • 373
  • 1
  • 2
  • 7
  • 2
    `Student1.name == Student2.name` [doesn't look right](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Nov 29 '14 at 15:15
  • 2
    When doing string comparison you should use .equals so in your case if (Student1.name.equals (Student2.name)){ } – chennighan Nov 29 '14 at 15:18
  • Serialization is about representing a Java Object not about comparing it. – Willi Mentzel Nov 29 '14 at 15:19
  • If you really need to check all fields for equality, I think you just have to compare them one by one. Keep in mind that comparison is different for several types (e.g. `int` can be compared with `==`, `Integer` should be compared with `integer1.equals(integer2)` and String can be compared with `equals` or `equalsIgnoreCase`). But maybe you can eliminate several fields, depending on what you need: 2 students may be only one person if they equal in age, name and address, while 2 students may be equally strong when having the same grades (but different names)... – GameDroids Nov 29 '14 at 15:19
  • @GameDroid for Integer you can use == if you type cast to primitive int. – chennighan Nov 29 '14 at 15:22
  • 1
    @ChrisHennighan: true, but this would be comparing two `int` and not two `Integer` wouldn't it :) – GameDroids Nov 29 '14 at 15:24
  • @GameDroid correct, although I've seen it confused multiple times so I thought it was noteworthy – chennighan Nov 29 '14 at 15:29

4 Answers4

2

If you are using an IDE (like Eclipse), then you would not need to worry.

For Eclipse :

  1. Right Click any white area in your code
  2. Click on "Generate equals and hashCode method
  3. Select fields you want in your equals method

This will automatically create your required equals method.

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
1

If you are willing to use external libraries there is a tool in Apache commons that helps you implement the equals() method using reflection. It is called EqualsBuilder:

Sample usage:

public boolean equals(Object obj) {
   return EqualsBuilder.reflectionEquals(this, obj);
}
Keppil
  • 45,603
  • 8
  • 97
  • 119
  • 1
    I think the OP has to grasp the principle of what equality means first. No need for third party libraries. – Willi Mentzel Nov 29 '14 at 15:22
  • 1
    I up voted your answer because I didn't get your down vote either – chennighan Nov 29 '14 at 15:24
  • @ChrisHennighan No discussion about up- and downvotes please. This matter is private! – Willi Mentzel Nov 29 '14 at 15:24
  • 1
    @haywire: I think it is perfectly fine to discuss it, as long as a civil tone is maintained. – Keppil Nov 29 '14 at 15:26
  • @ChrisHennighan In general it is discouraged to tell if you casted an up- or downvote, critique is always very welcome. – Willi Mentzel Nov 29 '14 at 15:29
  • As Apache says on their page: *"Two Objects that compare as equals must generate the same hash code, but two Objects with the same hash code do not have to be equal."* – So when it is absolutely necessary to compare hundreds of values, this one sounds like an awesome library. Thanks for that – GameDroids Nov 29 '14 at 15:31
0

I think you are need to understand what equality actually means

Well if you mean that two object are equal (even if they point to different memory location)when their all fields(are equal) then you have to compare all fields. Whether you do it manually or IDE does it for you or third party library, thats the different matter

If you mean that two object are equal only if their point to same memory location, then you can go for == comparison

M Sach
  • 33,416
  • 76
  • 221
  • 314
0

You should use equals() to compare Strings, but most importantly you should just use the generate equals() and hashCode() function in your IDE and see what that generates for you. That's a working, fool-proof and simple solution

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428