两个list比较的问题

寻求解答:

现在有个两个list

allList和errorList

allList里是300条数据,每个元素都是一个对象。

errorList里是15条数据,每个元素都是经过判断allList里不符合一些条件的元素,放进来的。

我想找到所有符合条件的数据validList
所使用的方法是

HrMEvaluationOld是我自定义的一个对象。

[code="java"]
for (Iterator iterator = allList.iterator(); iterator.hasNext();) {
HrMEvaluationOld evaluation = (HrMEvaluationOld) iterator.next();
if( !errorList.contains(evaluation)) {
validList.add(evaluation);
}
}
[/code]
为什么找出来的结果,是所有的记录都是符合条件的,根本没有找到allList里有包含errorList元素呢。

[code="java"] public boolean contains(Object o) {
return indexOf(o) >= 0;
}

/**
 * Returns the index of the first occurrence of the specified element
 * in this list, or -1 if this list does not contain the element.
 * More formally, returns the lowest index <tt>i</tt> such that
 * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
 * or -1 if there is no such index.
 */
public int indexOf(Object o) {
if (o == null) {
    for (int i = 0; i < size; i++)
    if (elementData[i]==null)
        return i;
} else {
    for (int i = 0; i < size; i++)
    if (o.equals(elementData[i]))
        return i;
}
return -1;
}

[/code]
以上是JDK的源码
默认情况下,调用contains方法是依次比较查找,对于对象类型,会调用对象的equals方法来判断。
而对象的equals方法是比较引用。
LZ的errorList里的东西应该不是从allList里来的,不然不可能会出现上述的问题。

如果出于设计,确实想要实现让两个引用地址不同但是其中内容相同的对象比较结果为相同的话,需要让HrMEvaluationOld重写equals方法。