简体   繁体   中英

Method “contains”.Java List

I have Spring MVC appication. I have entity class Examination. I overridden method eqauls so I could use method contains of List interface. When i try to add new exam, I look if i have already added it. But when i pass examination object to mathod contains, I always have different students. For example: I need to add exam to Student Jone. I try to add it and get another information: Kate : Jone, instead of Jone : Jone. I do not know why i happens because i pass examination object when i set student as Jone.

@Override
public boolean equals(Object arg) {
    Examination exam = (Examination) arg;

    System.out.println(exam.getStudent().getStudentFullName() + ":" + this.getStudent().getStudentFullName());

    if (!this.subject.getSubjectTitle().equals(exam.getSubject().getSubjectTitle()))
        return false;
    else 
        return true;
}

piece of code where i try to add exam

            examination.setStudent(currentStudent); // set student
            examination.setSubject(subjectExam); // set subject

            if(es.selectAllExams().contains(examination)) {
                return "error";
            } else {
                es.insertExam(examination); // add to database
                return "success";
            }

In the equals method you are comparing only titles, not the student name. So if you have two examinations with same title, but different student name they are equal (based on your equals method). Compare also students in the equals method and you should be good. In general it is good practice to override both equals and hashcode methods.

Your implementation of equals method is in general not following best practices for overriding equals method. Google a bit for "java equals method best practices" - you'll find something like this : http://javarevisited.blogspot.sk/2011/02/how-to-write-equals-method-in-java.html

If you are lazy to write your own equals or hashcode methods (or you have other reasons) you can use :

http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/builder/EqualsBuilder.html

or

http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/HashCodeBuilder.html

You say you override equals()

so I could use method contains of List interface

However, you don't have to override that method to use contains() . There's a default implementation that's suitable for most purposes; it boils down to "are these objects the same instance?". As a previous responder pointed out, you're breaking this logic with your implementation; all examinations with the same title will be considered the same object, so as long as your list has one examination with the same title as the one you're trying to add, the contains() check will always return true , and you'll never be able to add another one.

If you do want equality to be based on the title and student in question, then the previous answer is correct - you'll want to override both hashCode() and equals() , making sure you consider all fields important to an examination's identity in both methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM