Abstractset source code analysis – java8
•
Java
< H4 id = "13 methods" > 1.3 methods
< H4 id = "2ash value" > 2 Hash value
Hash = sum (hash value of each element) if the element is null, the default hash value is 0
3. removeAll(Collection
< H4 id = "4 source code analysis" > 4 Source code analysis
package sourcecode.analysis;
/** * @Author: cxh * @CreateTime: 18/3/21 20:42 * @ProjectName: JavaBaseTest */ import java.util.*; /** * AbstractSet是set接口的骨架实现,它减少了实现set接口的最少操作. * * 通过扩展此类来实现集合的过程与通过扩展AbstractCollection来实现集合的过程相同, * 不同之处在于此类的所有子类中的所有<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>和构造<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>都必须遵守由 Set接口 强加的额外约束。 * (例如,add<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>不允许将<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>对象的多个实例<a href="https://www.jb51.cc/tag/tianjia/" target="_blank" class="keywords">添加</a>到集合中) * * 注意:AbstractSet并没有覆盖任何AbastractCollection类. * 它仅仅<a href="https://www.jb51.cc/tag/tianjia/" target="_blank" class="keywords">添加</a>了equals()和hashcode()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>. * * AbstractSet是java集合框架中的一员. * * @since 1.2 */ public abstract class AbstractSet<E> extends AbstractCollection<E> implements java.util.Set<E> { //唯一构造器<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>,目的是为了子类对其<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>. protected AbstractSet() { } /*-------比较和hash相关的<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>--------*/ //常见的equals执行步骤 public boolean equals(Object o) { //o是否为当前对象 if (o == this) return true; //类型是否为set if (!(o instanceof java.util.Set)) return false; //强制转化 Collection<?> c = (Collection<?>) o; //比较size if (c.size() != size()) return false; try { //<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>AbstractCollection的<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a> return containsAll(c); } catch (ClassCastException unused) { return false; } catch (NullPointerException unused) { return false; } } //hash值=sum(所有元素的hash值) 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) { //c不能为空 Objects.requireNonNull(c); //set结构更改标志位:初始化为false boolean modified = false; //如果set的大小>参数集合c的大小 if (size() > c.size()) { //迭代<a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a>,并更新modified标志位 for (Iterator<?> i = c.iterator(); i.hasNext(); ) modified |= remove(i.next()); } else { //如果set的大小<参数集合c的大小,则set进行迭代 for (Iterator<?> i = iterator(); i.hasNext(); ) { if (c.contains(i.next())) { //<a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a>上一次迭代访问的元素,达到<a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a>目的 i.remove(); //更改<a href="https://www.jb51.cc/tag/xiugai/" target="_blank" class="keywords">修改</a>标志位modified modified = true; } } } return modified; } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码