一 AbstractSet继承关系
二 内部结构
三 继承方法
public boolean equals(Object o) { if (o == this) //首先判断是否为当前Set本身 return true; if (!(o instanceof Set)) //如果不是Set类型,则返回false return false; Collection<?> c = (Collection<?>) o; //转化为集合 if (c.size() != size()) //如果集合大小不一样则返回false return false; try { return containsAll(c); //再判断是否包含所有的o中的元素 } catch (ClassCastException unused) { return false; } catch (NullPointerException unused) { return false; } } 计算哈希值,遍历每一个对象的哈希值相加 public int hashCode() { int h = 0; Iterator<E> i = iterator(); while (i.hasNext()) { E obj = i.next(); if (obj != null) h += obj.hashCode(); } return h; } public boolean removeAll(Collection<?> c) { Objects.requireNonNull(c); boolean modified = false; if (size() > c.size()) { //当前集合较大,通过遍历小集合的进行删除 for (Iterator<?> i = c.iterator(); i.hasNext(); ) modified |= remove(i.next()); } else { //参数集合较大,反过来遍历当前集合 for (Iterator<?> i = iterator(); i.hasNext(); ) { if (c.contains(i.next())) { i.remove(); modified = true; } } } return modified; }