我有一个班级 Abc 如下
public class Abc {
int[] attributes;
Abc(int[] attributes){
this.attributes = attributes;
}
}
覆盖Abc
哈希码如下
@Override
public int hashCode() {
int hashCode = 0;
int multiplier = 1;
for(int i = attributes.length-1 ; i >= 0 ; i++){
hashCode = hashCode+(attributes[i]*multiplier);
multiplier = multiplier*10;
}
return hashCode;
}
我使用上面的类来创建一个对象列表,我想比较这两个列表是否相等,即具有相同属性的对象的列表。
List<Abc> list1 ;
list1.add(new Abc(new int[]{1,2,4}));
list1.add(new Abc(new int[]{5,8,9}));
list1.add(new Abc(new int[]{3,4,2}));
List<Abc> list2;
list2.add(new Abc(new int[]{5,8,9}));
list2.add(new Abc(new int[]{3,4,2}));
list2.add(new Abc(new int[]{1,2,4}));
如何在迭代每个列表的情况下比较上述两个列表。还有没有更好的方法来覆盖 hashcode ,以便具有相同属性(值和顺序)的两个类应该相等。