- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有 2 个类(class)“学生”和“小组”。小组有一组学生。 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]>
最佳答案
对我来说效果很好:
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));
关于java - 为什么在定义 hashCode() 和 equals() 后我的代码仍然比较链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47431385/
class UserScoring implements Comparable { User user; int score; UserScoring(
当重写 Java 中的 equals() 和 hashcode() 方法时,为什么不经常使用它: public int hashCode() { return (int) this.hashC
给定java Object#hashCode文档快照: As much as is reasonably practical, the hashCode method defined by class
下面的代码(sign.hashCode())是给我签名的hashCode还是内存中对象的hash? try { PackageInfo packageInfo = getPackageMana
考虑: String[] segments = {"asdf", "qwerty", "blahblah", "alongerstring", "w349fe3434"}; String fullSt
在审查大型代码库时,我经常遇到这样的情况: @Override public int hashCode() { return someFieldValue.hashCode(); } 程序员不
在以下情况下,与下面的函数发生 HashCode 冲突的可能性有多大。 key[0]、key[1]、key[2]、key[3] 的随机整数值 使用具有以下约束的随机键值 键[0] <1,000,000
从 Java 7 开始,我们有了 o.hashCode(); Objects.hashCode(o); Objects.hash(o); 前两个与空检查大致相同,但最后一个是什么? When a si
这个问题已经有答案了: Objects.hash() vs Objects.hashCode(), clarification needed (3 个回答) 已关闭 6 年前。 一个简单、简短的问题:
我是否需要使用super.hashcode()来计算this.hashcode()? IDE(例如 IntelliJ Idea)可以生成 equals 和 hashcode。它可以使用 java.ut
class A { } class B extends A { void m1(){ System.out.println(this.hashCode());
我查看了Arrays.hashCode(char[] c)的源代码 我不太确定它适用的算法是否在所有情况下都能正常工作。 public static int hashCode(int a[])
我有两个表具有一对一的关系,如下所示: @Entity @Data @NoArgsConstructor @AllArgsConstructor public class Book { @Id
为什么stringObject的hashcode是我提供的字符串? String s = new String(); // here the hascode is 0. 但是当我获得我创建的某个对象的
public abstract class HolidayPackageVariant { private HolidayPackage holidayPackage; private String
这两个代码片段有什么区别? 片段 1: Object o = new Object(); int i = Objects.hashCode(o); 片段 2: Object o = new Objec
在 Java 8 中有一个类 java.util.Objects,其中包含 hashCode() 方法。同时 Google Guava 19 包含 com.google.common.base.Obj
我的一个类(class)中有以下方法。它只是 HashMap 的公共(public)包装器(名为 teamOfPlayer,具有 Player 对象的键和 Integer 对象的值),仅此而已。 pu
我在这里做错了什么? @Override public int hashCode() { HashCodeBuilder has
我有以下程序。 Employee employee1 = new Employee("Raghav1", 101); Employee employee2 = new Employee("Raghav
我是一名优秀的程序员,十分优秀!