博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HashSet源码分析2
阅读量:7258 次
发布时间:2019-06-29

本文共 2903 字,大约阅读时间需要 9 分钟。

hot3.png

package com.test1;import java.util.HashSet;import java.util.Iterator;import java.util.Set;public class SetTest {    public static void main(String[] args) {        /*         * Set
set = new HashSet<>(); * System.out.println(set.add("abc")); * System.out.println(set.add("xyz")); * System.out.println(set.add("abc")); * * for (Iterator
it = set.iterator(); it.hasNext();) { * System.out.println(it.next()); } */ /* * String a = "abc"; String b = "abc"; System.out.println(a.hashCode()); * System.out.println(b.hashCode()); */ Set
set2 = new HashSet<>(); set2.add(new People("zhangsan")); set2.add(new People("lisi")); set2.add(new People("zhangsan")); for (Iterator
it = set2.iterator(); it.hasNext();) { System.out.println(it.next().getName()); } }}class People { String name; public People(String name) { this.name = name; } public String getName() { return this.name; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof People) { People people = (People) obj; if(this.name.equals(people.getName())); return true; } return false; } @Override public int hashCode() { return this.name.hashCode(); }}

我们来看HashSet的构造方法:

/**     * Constructs a new, empty set; the backing HashMap instance has     * default initial capacity (16) and load factor (0.75).     */    public HashSet() {        map = new HashMap<>();    }

竟然是new HashMap<>();

关于HashSet与HashMap之间的关系:

HashSet是由HashMap来实现的。HashSet里面的几乎所有的方法都是由HashMap实现的

这个HashMap的key就是放进HashSet中的对象,value就是一个Object类型的对象,当调用HashSet的add方法时,实际上是向HashMap中增加了一行(key-value对),该key就是向HashSet中增加的那个对象,该行的value就是一个Object类型的对象。HashMap底层采用数组维护(数组中每个元素都是一个Map.Entry对象),调用增加的那个对象的hashCode方法,得到一个hashCode值,然后根据该值计算出一个数组的下标索引(计算出数组中的一个位置),将准备添加到Map中的对象与该位置处的对象进行比较(equals方法),如果相同,那么就向该位置处的那个对象(Map.Entry类型)的value值替换掉,否则沿着该Entry链继续重复上述过程,如果链的最后依然没有找到相同的对象,那么这个时候就将该对象添加到数组中,将数组中该位置处的Entry对象链接到这个对象后面:对于HashSet与HashMap来说,这样做是为了提高查找的效率,使得查找时间不随着Set或Map的大小而改变。

查看add方法

/**     * Adds the specified element to this set if it is not already present.     * More formally, adds the specified element e to this set if     * this set contains no element e2 such that     * (e==null ? e2==null : e.equals(e2)).     * If this set already contains the element, the call leaves the set     * unchanged and returns false.     *     * @param e element to be added to this set     * @return true if this set did not already contain the specified     * element     */    public boolean add(E e) {        return map.put(e, PRESENT)==null;    }

 

转载于:https://my.oschina.net/duanvincent/blog/775119

你可能感兴趣的文章
Android P2P语音通话实现(思路探讨)
查看>>
eclipse中关联文件设置方法
查看>>
php中的mysql_fetch_row,mysql_fetch_array,mysql_fetch_object
查看>>
JPHP试用笔记
查看>>
NHibernate 中使用 nvarchar(max) 类型
查看>>
电商大数据——用数据驱动电商和商业案例解析
查看>>
ZOJ 3635 Cinema in Akiba (第一次组队) 树状数组+二分
查看>>
数据结构很重要
查看>>
搜狗实习结束了
查看>>
unity3d中资源文件从MAX或者MAYA中导出的注意事项
查看>>
【LeetCode】128. Longest Consecutive Sequence
查看>>
使用phonegap + appframework2.0框架
查看>>
Linux命令工具基础02 文件及目录管理
查看>>
Linux 安装配置maven3.0 以及搭建nexus私服
查看>>
给Select赋值 innerHTML 不兼容IE6\IE7\IE8\IE9
查看>>
JAVA中的四种引用
查看>>
盘点前 10 名的免费跨浏览器测试工具
查看>>
Asp.Net MVC5入门学习系列③
查看>>
数学图形(1.27) 花
查看>>
Orchard模块开发全接触5:深度改造前台第二部分
查看>>