如何使用包含相同类型对象的两个列表从对象生成哈希码
How to generate a hashcode from object with two list containing the same type of objects
假设我有一个class(同样的方法也存在):
public class SomeClassA {
private int a;
private int b;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + a;
result = prime * result + b;
return result;
}
}
还有一个class:
public class SomeClassB {
List<SomeClassA> firstList;
List<SomeClassA> secondList;
如果两个对象在 firstList 中具有相同的对象而在 secondList 中具有相同的对象,我如何构建哈希码以便将它们视为相等。
//汉克
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + firstList.hashCode();
result = prime * result + secondList.hashCode();
return result;
}
假设我有一个class(同样的方法也存在):
public class SomeClassA {
private int a;
private int b;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + a;
result = prime * result + b;
return result;
}
}
还有一个class:
public class SomeClassB {
List<SomeClassA> firstList;
List<SomeClassA> secondList;
如果两个对象在 firstList 中具有相同的对象而在 secondList 中具有相同的对象,我如何构建哈希码以便将它们视为相等。
//汉克
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + firstList.hashCode();
result = prime * result + secondList.hashCode();
return result;
}