Home java How to Compare 2 objects

How to Compare 2 objects

Author

Date

Category

How to compare 2 objects without noodle code?

For example, this class

Public Class MyObject {
  String Name;
  String Surname;
  INT AGE;
  boolean isalive;
}

and not to write something like

boolean iSequals (MyObject O1, MyObject O2) {
  if (! o1.name.equals (O2.Name)) {
    RETURN FALSE;
  }
  If (! O1.Surname.equals (O2.Surname)) {
    RETURN FALSE;
  }
  if (o1.age! = (O2.age)) {
    RETURN FALSE;
  }
  RETURN O1.ISALIVE == O2.ISALIVE;
}

Answer 1, Authority 100%

You need to override the Object :: Equals () method.

You can automatically do it through Intellij IDEA:
Code - & gt; Generate - & gt; Equals and Hashcode .

You should get something similar to it:

@ override
Public Boolean Equals (Object O) {
  if (this == o) Return True;
  if (O == NULL || getClass ()! = O.Getclass ()) Return False;
  MYOBJECT MYOBJECT = (MyObject) O;
  Return Objects.equals (Name, MyObject.name) & amp; & amp;
      Objects.equals (Surname, MyObject.surname) & amp; & amp;
      Age == MyObject.age
      isalive == myobject.isalive;
}

It can be compared as follows:

myobject1.equals (myobject2);


Answer 2, Authority 100%

compararator & lt; myobject & gt; compararator = compararator.comparing (MYOBJECT :: getName)
                      .thencomparing (MYOBJECT :: GetSurname)
                      .thencomparing (MYOBJECT :: IsALIVE)
                      .Thencomparingint (MYOBJECT :: Getage);
Comparator & lt; MyObject & gt; nullsafe = compararator.nullsfirst (Comparator);
Return nullsafe.compare (O1, O2) == 0;

Answer 3, Authority 100%

If you want a universal method, then you can somehow, reflectay, but you probably know that it is slow and that’s all this:

Private Static Boolean EQ (Object O1, Object O2) {
  Try {
    if (O1 == NULL & amp; & amp; o2 == null) Return True;
    if (O1 == NULL || O2 == NULL) RETURN FALSE;
    if (! o1.getclass (). Equals (O2.getclass ())) RETURN FALSE;
    For (Field Field: O1.getclass (). getdeclaredfields ()) {
      Field.setAccessible (True);
      Object v1 = field.get (O1);
      Object v2 = field.get (O2);
      if (v1 == null & amp; & amp; v2 == null) continue;
      if (v1 == null || v2 == null) Return False;
      if (! v1.equals (v2)) Return False;
    }
  } Catch (illegalaccessException E) {
    RETURN FALSE;
  }
  RETURN TRUE;
}

PS: In general, I do not recommend using this approach, it is better to generate a method through IDE


Answer 4, Authority 67%

If you need to compare objects, you need to override the Equals () method .

@ override
Public Boolean Equals (Object O) {
  if (this == o) Return True;
  if (! (o Instanceof MyObject)) RETURN FALSE;
  MYOBJECT THAT = (MyObject) O;
  Return isalive == that.isalive & amp; & amp;
      Age == That.age & amp; & amp;
      name.equals (that.name) & amp; & amp;
      Surname.equals (that.surname);
}

Answer 5, Authority 33%

Using the Apache Commons Lang and reflection can even be in one line

return new equalsbuilder (). ReflectionEquals (O1, O2);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions