Java ArrayList, how to ignore a value from the object when using contains()

I have a class like that:

public class Student implements Serializable{
  private String name;
  private int age;
  private Image img;
}

I store a few students in an ArrayList and write them to a file. When I restart the application I load them from that file again. However, the Image img variable is constantly changing. That means when I use arrayList.contains(studentA) it’s not the same object anymore, so arrayList.remove(studentA) won’t work.

Is there an easy way to either: Only check for the name of the student or ignore the Image field when using contains()?

Solution:

Yes.

Just implement the equals/hashcode without the Image attribute.

public class Student implements Serializable {

    private String name;
    private int age;
    private Image img;

    @Override
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {

        return Objects.hash(name, age);
    }

}