经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
jdk源码浅读-HashSet
来源:cnblogs  作者:姓码名农小小  时间:2018/11/9 11:23:45  对本文有异议

  通过阅读源码发现,HashSet底层的实现源码其实就是调用HashMap的方法实现的,所以如果你阅读过HashMap或对HashMap比较熟悉的话,那么阅读HashSet就很轻松,也很容易理解了。我之前也写了一篇关于hashMap源码阅读的文章,可以点击这里查看。

  使用过HashSet的都清楚它保存的元素是不可以重复的,其实HashSet的元素都是保存在HashMap的key中的,而HashMap的key是没有重复的。

  构造函数

  1. /**
  2. * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
  3. * the specified initial capacity and the specified load factor.
  4. *
  5. * @param initialCapacity the initial capacity of the hash map
  6. * @param loadFactor the load factor of the hash map
  7. * @throws IllegalArgumentException if the initial capacity is less
  8. * than zero, or if the load factor is nonpositive
  9. */
  10. public HashSet(int initialCapacity, float loadFactor) {
  11. map = new HashMap<>(initialCapacity, loadFactor);
  12. }

  HashSet的构造方法都是直接调用了HashMap的构造方法,HashSet有很多个构造方法全部都是直接调用了HashMap的构造方法。

  add方法

  1. /**
  2. * Adds the specified element to this set if it is not already present.
  3. * More formally, adds the specified element <tt>e</tt> to this set if
  4. * this set contains no element <tt>e2</tt> such that
  5. * <tt>(e==null&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;e.equals(e2))</tt>.
  6. * If this set already contains the element, the call leaves the set
  7. * unchanged and returns <tt>false</tt>.
  8. *
  9. * @param e element to be added to this set
  10. * @return <tt>true</tt> if this set did not already contain the specified
  11. * element
  12. */
  13. public boolean add(E e) {
  14. return map.put(e, PRESENT)==null;
  15. }

  contains方法

  1. /**
  2. * Returns <tt>true</tt> if this set contains the specified element.
  3. * More formally, returns <tt>true</tt> if and only if this set
  4. * contains an element <tt>e</tt> such that
  5. * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
  6. *
  7. * @param o element whose presence in this set is to be tested
  8. * @return <tt>true</tt> if this set contains the specified element
  9. */
  10. public boolean contains(Object o) {
  11. return map.containsKey(o);
  12. }

  remove方法

  1. /**
  2. * Removes the specified element from this set if it is present.
  3. * More formally, removes an element <tt>e</tt> such that
  4. * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>,
  5. * if this set contains such an element. Returns <tt>true</tt> if
  6. * this set contained the element (or equivalently, if this set
  7. * changed as a result of the call). (This set will not contain the
  8. * element once the call returns.)
  9. *
  10. * @param o object to be removed from this set, if present
  11. * @return <tt>true</tt> if the set contained the specified element
  12. */
  13. public boolean remove(Object o) {
  14. return map.remove(o)==PRESENT;
  15. }

 其他的方法也都是直接调用HashMap的方法,所以在这里就不用贴出来了。

  1、只要弄懂HashMap就很容易明白HashSet了,可以参考这篇文章:jdk源码浅读-HashMap https://www.cnblogs.com/rainple/p/9927263.html

  2、我自己在看源码的时候也手写了HashMap、HashSet等数据结构的类,大家可以下载下来参考一下,有不懂或不理解的地方可以问我,如果有什么问题也随时欢迎骚扰。项目地址:https://github.com/rainple1860/MyCollection

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号