为什么在定义hashCode()和equals()之后,我的代码仍会比较链接

时间:2017-11-22 09:48:32

标签: java equals hashcode

我有2个班级学生和小组。小组有一套学生。 hashCode()equals()都覆盖了两者。

然后我在群组中填充Set<Student>。为什么两个相同的组(相同的ID,名称,Set<Student>等)是不同的?当我从群组的Set<Student>hashCode()删除equals()时,群组会变得相同。但这是两个相同的Set<Student>。我认为在Set<Student>的情况下,它会比较链接。我不明白这一点,例如String name是一个对象而Set<Student>是一个Object。如果我同时将hashCode()equals()都包含在内,则会将String name的实际值与Set<Student>的链接进行比较。

Student.java:

public class Student {  

    private int id;
    private String firstName;
    private String lastName;

    public Student(int id, String firstName, String lastName) {     
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }   

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    @Override
    public boolean equals(final Object other) {
        if (this == other) {
            return true;
        }
        if (other == null) {
            return false;
        }
        if (!getClass().equals(other.getClass())) {
            return false;
        }
        Student castOther = (Student) other;
        return Objects.equals(id, castOther.id) 
                && Objects.equals(firstName, castOther.firstName)
                && Objects.equals(lastName, castOther.lastName);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, firstName, lastName);
    }       
}

Group.java:

public class Group {

    private int id;
    private String name;    
    private Set<Student> students;

    public Group(int id, String name) {     
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Set<Student> getStudents() {
        return students;
    }

    public void setStudents(Set<Student> students) {
        this.students = students;
    }

    public String getName() {
        return name;
    }

    @Override
    public boolean equals(final Object other) {
        if (this == other) {
            return true;
        }
        if (other == null) {
            return false;
        }
        if (!getClass().equals(other.getClass())) {
            return false;
        }
        Group castOther = (Group) other;
        return Objects.equals(id, castOther.id) 
                && Objects.equals(name, castOther.name)
                && Objects.equals(students, castOther.students);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, students);
    }
}

GroupDaoImpl.java:

Set<Group> retrieveAll() throws DaoException {      
    String sql = "select * from groups;";               
    Set<Group> groupSet = new HashSet<>();

    try (Connection connection = daoFactory.getConnection();
            PreparedStatement preparedStatement = 
            connection.prepareStatement(sql)) {         
        preparedStatement.execute();
        ResultSet resultSet = preparedStatement.getResultSet();         

        while (resultSet.next()) {
            int id = resultSet.getInt("group_id");
            String name = resultSet.getString("name");              
            Group group = new Group(id, name);              
            group.setStudents(studentDao.retrieveByGroupId(id));                
            groupSet.add(group);
        }                       
    } catch (Exception e) {         
        throw new DaoException(e);          
    } 
    return groupSet;
}

StudentDaoImpl.java:

Set<Student> retrieveByGroupId(int groupId) throws DaoException {       
    String sql = "select * from student where group_id = ?;";       
    Set<Student> studentSet = new HashSet<>();

    try (Connection connection = daoFactory.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
        preparedStatement.setInt(1, groupId);
        preparedStatement.execute();            
        ResultSet resultSet = preparedStatement.getResultSet();

        while (resultSet.next()) {
            int id = resultSet.getInt("student_id");
            String firstName = resultSet.getString("first_name");
            String lastName = resultSet.getString("last_name");             
            Student student = new Student(id, firstName, lastName);             
            studentSet.add(student);
        }                       
    } catch (Exception e) {
        throw new DaoException(e);          
    } 
    return studentSet;
}

GroupDaoImplTest.java:

public class GroupDaoImplTest {

private GroupDaoImpl groupDao = new GroupDaoImpl();
private static Set<Group> groupSet = new HashSet<>();
private static Group group;

@BeforeClass
public static void setUp() throws DaoException {
    StudentDaoImpl studentDao = new StudentDaoImpl();
    group = new Group(1, "Group 11");
    group.setStudents(studentDao.retrieveByGroupId(1));
    groupSet.add(group);        
    groupSet.add(new Group(2, "Group 12")); 
}

@Test
public void testInsert() throws DaoException {
    groupDao.insert(new Group(1, "Group 13"), 2);       
}

@Test
public void testUpdate() throws DaoException {
    groupDao.update(new Group(15, "Group 11"));     
}

@Test
public void testDelete() throws DaoException {
    groupDao.delete(new Group(16, "Group 11"));
}

@Test
public void testRetrieve() throws DaoException {        
    assertThat(groupDao.retrieve(1), is(group));        
}

@Test
public void testRetrieveByCathedraId() throws DaoException {        
    assertThat(groupDao.retrieveByCathedraId(1), is(groupSet));
}

@Test
public void testRetrieveAll() throws DaoException {             
    assertThat(groupDao.retrieveAll(), is(groupSet));
}

}

当我执行testRetrieveAll()时,我得到:

java.lang.AssertionError: 
Expected: is <[university.domain.Group@1ef58100, university.domain.Group@1ef5849f]>
     but: was <[university.domain.Group@1ef58100, university.domain.Group@1ef5849f]> 

3 个答案:

答案 0 :(得分:1)

我工作得很好:

Group g1 = new Group(1, "a");
Set<Student> s1 = new HashSet<>();
s1.add(new Student(1, "a", "b"));
g1.setStudents(s1);

Group g2 = new Group(1, "a");
Set<Student> s2 = new HashSet<>();
s2.add(new Student(1, "a", "b"));
g2.setStudents(s2);

System.out.println(g1.equals(g2));

答案 1 :(得分:0)

我认为问题是测试类中students的{​​{1}}字段为group,而null返回的students字段为groupDao.retrieveAll()字段}总是有一个非null students集。如果组中没有学生,则其students字段为空(不包含学生),不为空。

Objects.hash()为空字段提供哈希码0,空集的哈希码也为0.因此,这两个组具有相同的哈希码,但它们不相等。

答案 2 :(得分:-1)

打电话给你时,你期待什么结果 Objects.equals(学生,castOther.students)?

我想,只有当两个Group实例的学生字段正好引用Set的一个实例时,才会返回 true