Home > Mobile >  An Easier Way to Detect Differences Between Objects? (Java)
An Easier Way to Detect Differences Between Objects? (Java)

Time:04-19

For instance, I have an arraylist called StudentListA and StudentListB that contains many students.

StudentListA = {Student 1, Student 2, Student 3....}

StudentListB = {Student A, Student B, Student C....}

In each student, they have their own attributes such as name, address, gpa etc. How do I compare if Student 1 has the same attribute value with Student A, and so on.

For now I am thinking of something like this:

int i = 0;
for (Student student : StudentListA){
   if(student.getName().equals(studentListB.get(i).getName() && 
      student.getAddress().equals(studentListB.get(i).getAddress()....){
        //Do smtg
   }
i  ;

}

Is there an easier way to do this? Because I have quite a handful of attributes. I want to know if the first and second list have the exact same students or not.

CodePudding user response:

You should move comparison of attributes to Student#equals method. Then you will use Student#equals in the cycle:

for (Student student : StudentListA){
   if(student.equals(studentListB.get(i))){
        //Do smtg
   }
i  ;

}

You can look at Lombock EqualsAndHashCode - it would be easiest way to generate the #equals method.

If you aren't allowed to use lombock, then you will have to write the Student#equals yourself.

In real world it's rare to write implementation for #equals because IDE can help you to generate it. E.g. this is what Idea generated for me:

public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Student student = (Student) o;

            if (this.name != null ? !name.equals(student.name) : student.name != null) return false;

            return true;
        }

CodePudding user response:

Is there an easier way to do this? Because I have quite a handful of attributes.

There is no shortcut in Java itself for testing whether two distinct objects have all corresponding attributes equal to each other. You can, however, write a method that performs such a test, so that you can reuse it, and so that your code is better factored. You can also choose that as your definition of value equality for objects of a given type, by making the method implementing that comparison an override of Object.equals().

If you do override Object.equals() then be sure to override Object.hashcode() as well, to maintain consistency with equals(). Objects that test equal to each other should have the same hash code, though the reverse is not necessarily true.

CodePudding user response:

  1. As suggested in other post, Key implementation is to override equals and hashCode, that is must. This can be easily done code generation using IDE/eclipse.
  2. Implements the java.lang.Comparable interface
  3. Override the compareTo method

//sample implementation,

public int compareTo(Student o){
  
if (o.hashCode() < this.hashCode()){return -1}
 else if(o.hashCode() > this.hashCode()) {return 1}

  return 0;

}

  1. Use Collections.sort for sorting

  2. Now you have two sorted list, you can use easily compare two sorted list.

  • Related