0

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.

hen
  • 95
  • 1
  • 6
  • There is a reason why we have the equals and hashcode methods in java. You don't have to compare individual properties override the equals and hashcode in the student class and use equals on the student object. And if the reference is the same you can use the == operator. – Tarun Apr 18 '22 at 14:43
  • @JohnBollinger For studentListA and studentListB to be the same, the Student 1 and Student A name, address, gpa need to be the same. Then Student 2 and Student B name, address, gpa need to be the same as well. so on and so forth – hen Apr 18 '22 at 14:44
  • Have you investigated existing questions on SO, such as [How to compare objects by multiple fields](https://stackoverflow.com/q/369512/12567365)? Do those questions help? – andrewJames Apr 18 '22 at 14:52

3 Answers3

0

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;
        }
Petr Aleksandrov
  • 1,434
  • 9
  • 24
  • Use an IDE to generate the equals method, or use the new ```record``` concept. IMO Lombok is an anti-pattern. – swpalmer Apr 18 '22 at 14:56
  • Not sure if i understood this, but I think I need to override the object equals method. However as shown in your code, on the last part, I seem to need to code the if statement for (getName(), getAddress()...) and compare each of these student's attributes one by one as well. – hen Apr 18 '22 at 15:14
0

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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0
  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.

kus
  • 446
  • 3
  • 7