使用聚合 Arraylist 覆盖 class 中的 equals 方法
Overide equals method in class with aggregation Arraylist
我需要覆盖 Java class 中的 equals()
和 hashCode()
方法。但是这个 class 包含一个对象列表。我应该如何实现比较 ArrayList
字段的代码部分?
代码如下:
public class FirstClass {
private List<SecondClass> list = new ArrayList<>();
equals() {
//how i should implement equals for list field?
}
hashCode() {
}
}
我只能补充一点,SecondClass
有很好的 equals
和 hashCode
方法。
我知道对称、过渡和其他重要的事情,但我只想关注 ArrayList
字段的这个问题。
ArrayList
(以及任何 List
实现)需要正确覆盖 equals
和 hashCode
,如 List
javadoc 中所述:
boolean java.util.List.equals(Object o)
Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.
int java.util.List.hashCode()
Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation:
int hashCode = 1;
for (E e : list) {hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());}
因此,您只需调用 list.equals()
和 list.hashCode()
。
public class FirstClass
{
private List<SecondClass> list = new ArrayList<>();
@Override
public boolean equals(Object other) {
if (this == other)
return true;
if (!(other instanceof FirstClass))
return false;
FirstClass fc = (FirstClass) other;
return list.equals(fc.list);
}
@Override
public int hashCode() {
return list.hashCode ();
}
}
这是假设 list
永远不会是 null
。否则,必须添加一些额外的检查。
我需要覆盖 Java class 中的 equals()
和 hashCode()
方法。但是这个 class 包含一个对象列表。我应该如何实现比较 ArrayList
字段的代码部分?
代码如下:
public class FirstClass {
private List<SecondClass> list = new ArrayList<>();
equals() {
//how i should implement equals for list field?
}
hashCode() {
}
}
我只能补充一点,SecondClass
有很好的 equals
和 hashCode
方法。
我知道对称、过渡和其他重要的事情,但我只想关注 ArrayList
字段的这个问题。
ArrayList
(以及任何 List
实现)需要正确覆盖 equals
和 hashCode
,如 List
javadoc 中所述:
boolean java.util.List.equals(Object o)
Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.
int java.util.List.hashCode()
Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation:
int hashCode = 1;
for (E e : list) {hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());}
因此,您只需调用 list.equals()
和 list.hashCode()
。
public class FirstClass
{
private List<SecondClass> list = new ArrayList<>();
@Override
public boolean equals(Object other) {
if (this == other)
return true;
if (!(other instanceof FirstClass))
return false;
FirstClass fc = (FirstClass) other;
return list.equals(fc.list);
}
@Override
public int hashCode() {
return list.hashCode ();
}
}
这是假设 list
永远不会是 null
。否则,必须添加一些额外的检查。