class 上的 lombok EqualsAndHashCode 有一个列表
lombok EqualsAndHashCode on a class which has a list
@EqualsAndHashcode
MyClass {
String property1;
List<NewClass> newClassList;
}
@EqualsAndHashcode
NewClass {
String abc;
String xyz;
}
如果我比较 MyClass 的两个对象(用@EqualsAndHashcode 注释)是否相等,是否会检查 newClassList 属性 的顺序?
通过使用 @EqualsAndHashcode
它将传播到使用 newClassList.equals(..)
方法,根据 java doc
Interface List
boolean 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.
如果您想要一个列表不检查顺序的自定义功能,那么您必须删除 @EqualsAndHashcode
并根据您的需要提供自己的 equals
方法。
您想要的功能将以简单的方式实现
(list1 != null && list2 != null && list1.size() == list2.size() && list1.containsAll(list2) && list2.containsAll(list1) ) || (list1 == null && list2 == null)
这将引导我们使用以下 equals 方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
NewClass newClass = (NewClass) o;
return Objects.equals(property1, newClass.property1) &&
((newClassList != null && newClass.newClassList != null && newClassList.size() == newClass.newClassList.size() && newClassList.containsAll(newClass.newClassList) && newClass.newClassList.containsAll(newClassList) ) || (newClassList == null && list2 == null));
}
别忘了手动覆盖 hashcode
方法。
@EqualsAndHashcode
MyClass {
String property1;
List<NewClass> newClassList;
}
@EqualsAndHashcode
NewClass {
String abc;
String xyz;
}
如果我比较 MyClass 的两个对象(用@EqualsAndHashcode 注释)是否相等,是否会检查 newClassList 属性 的顺序?
通过使用 @EqualsAndHashcode
它将传播到使用 newClassList.equals(..)
方法,根据 java doc
Interface List
boolean 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.
如果您想要一个列表不检查顺序的自定义功能,那么您必须删除 @EqualsAndHashcode
并根据您的需要提供自己的 equals
方法。
您想要的功能将以简单的方式实现
(list1 != null && list2 != null && list1.size() == list2.size() && list1.containsAll(list2) && list2.containsAll(list1) ) || (list1 == null && list2 == null)
这将引导我们使用以下 equals 方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
NewClass newClass = (NewClass) o;
return Objects.equals(property1, newClass.property1) &&
((newClassList != null && newClass.newClassList != null && newClassList.size() == newClass.newClassList.size() && newClassList.containsAll(newClass.newClassList) && newClass.newClassList.containsAll(newClassList) ) || (newClassList == null && list2 == null));
}
别忘了手动覆盖 hashcode
方法。