2

I have two objects of type MyObject. Their private fields have things like Integer, int, Long, long, String, etc -- no fancy sub-objects or anything to this effect.

I want to check to see if their fields are equal. I am not trying to check if they are the same object in memory (the same reference?), which they shouldn't be anyway since these two objects are from separate lists / created separately.

What is the correct way to check if all the fields of one object have the same values as all the other fields of the second object?

asked May 18, 2016 at 17:47
3
  • I don't really understand what the problem you're facing is. Do you feel you have "too many" fields for overriding .equals? Commented May 18, 2016 at 17:49
  • Override the boolean equals(Object o) method. Commented May 18, 2016 at 17:49
  • 1
    @Zircon I just want to make sure overriding .equals is the correct thing to do here, as opposed to doing object1 == object2 or object1.equals(object2) or implementing Comparable or Comparator or something. Too many options and I am unsure which is correct. Commented May 18, 2016 at 17:50

5 Answers 5

1

The best way is to override the method equals(Object) and let your IDE implements it for you.

answered May 18, 2016 at 17:53
Sign up to request clarification or add additional context in comments.

8 Comments

My IDE doesn't seem to have a way to implement it for me? (Android Studio). I just wrote @Override onpublic boolean equals(Object obj), checked if obj was an instance of myObject, typecasted it, and then returned a big and-chain of field comparisons. Is this all I have to do?
provide your class definition with your fields in your question and your java version, I will generate it for you
I do see a generate function in Android Studio actually but I don't know which Template to use(IntelliJ Default, Apache commons-lang, Apache commons-lang 3, Objects.equal and hashCode (Guava), java.util.Objects.equals and hashCode (java 7+)).
choose IntelliJ for example
or if you use java 8, you can use java.util.Objects.equals and hashCode (java 7+)
|
0

You need to override the equal method in your class and compare if the values of the two class are same return true else false. example:

obj1.equals(obj2);

in your class

@Override
public boolean equals(YourClass obj) {
 // compare the values of both objets.
 // return true on success;
}
answered May 18, 2016 at 17:57

Comments

0

Use Object.equals() to compare

answered May 18, 2016 at 17:58

Comments

0

Overriding Object's equals method is the best choice for your situation. You can either ensure the equality of every field of the object, or you can just compare a field or combination of fields that you know will be unique.

If you're using collections such as Maps, you might consider overriding hashcode using these fields instead, and then compare hashcodes in the equals method.

For your reference:

  • equals should always determine if two objects are "equal" in concept (which usually means they have the same fields).
  • == compares memory location, and thus equality between primitive types
  • Comparable interface is used to order an object "ahead" or "behind" another object; useful for sorting arrays and collections.
  • Comparator interface is similar to Comparable but uses a "third-party" object to compare two other objects. This Comparator can be passed into collection-sorting methods if you don't want your objects to be Comparable.
answered May 18, 2016 at 18:04

Comments

0

mDo as @Raj K suggested. For example, if you have:

public class MyObject {
 int i1;
 long l1;
 long i2;
 // and a bunch of c'tors and methods
}

then your equals() method would be something like

public Boolean equals( Object o ) {
 if ( o instanceof MyObject ) {
 MyObject m = (MyObject)o;
 return (i1==m.i1) && (l1==m.l1) && (l2==m.l2);
 }
 return false;
}

If any of your fields are Objects, you have to be a bit more careful, since you also have to first check for null's before checking whether

this.something.equals(m.something)
answered May 18, 2016 at 18:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.