62,614
社区成员
发帖
与我相关
我的任务
分享
接口Collection定义equals
interface Collection{
boolean equals(Object o);
}
接口Set继承了Collection
public interface Set<E> extends Collection<E>{
boolean equals(Object o);
}
class HashSet extends AbstractSet<E> implements Set<E>{
}
public abstract class AbstractSet<E> implements Set<E>{
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Set))
return false;
Collection c = (Collection) o;
if (c.size() != size())
return false;
try {
return containsAll(c);
} catch (ClassCastException unused) {
return false;
} catch (NullPointerException unused) {
return false;
}
}
}