简体   繁体   English

方法“包含”.Java列表

[英]Method “contains”.Java List

I have Spring MVC appication. 我有Spring MVC应用程序。 I have entity class Examination. 我有实体课考试。 I overridden method eqauls so I could use method contains of List interface. 我重写方法eqauls所以我可以使用包含List接口的方法。 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. 但是当我通过考试对象到mathod包含时,我总是有不同的学生。 For example: I need to add exam to Student Jone. 例如:我需要在Student Jone中添加考试。 I try to add it and get another information: Kate : Jone, instead of Jone : Jone. 我尝试添加它并获得另一个信息:Kate:Jone,而不是Jone:Jone。 I do not know why i happens because i pass examination object when i set student as Jone. 我不知道为什么我会发生,因为当我将学生设为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. 在equals方法中,您只比较标题,而不是学生姓名。 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. 比较equals方法的学生,你应该是好的。 In general it is good practice to override both equals and hashcode methods. 通常,优先考虑覆盖equals和hashcode方法。

Your implementation of equals method is in general not following best practices for overriding equals method. 您的equals方法的实现通常不遵循覆盖equals方法的最佳实践。 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 谷歌有点“java equals方法最佳实践” - 你会发现这样的东西: 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 : 如果你懒得编写自己的equals或hashcode方法(或者你有其他原因),你可以使用:

http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/builder/EqualsBuilder.html 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 http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/HashCodeBuilder.html

You say you override equals() 你说你重写equals()

so I could use method contains of List interface 所以我可以使用包含List接口的方法

However, you don't have to override that method to use contains() . 但是,你没有覆盖该方法使用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. 所有具有相同标题的考试都将被视为同一个对象,因此只要您的列表中有一个与您要添加的考试具有相同标题的考试,则contains()检查将始终返回true ,您将永远不能再添加另一个。

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. 如果你想平等的基础上讨论的题目和学生,那么以前的答案是正确的-你要同时重写hashCode()equals() ,确保您认为重要的是要检查的身份在各个领域两种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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