• HashSet代码分析


     HashSet (jdk 1.7)的继承关系如下:

    HashSet是使用HashMap实现的一个没有重复元素的集合。HashSet用法如下:

    HashSet<String> hashSet = new HashSet<String>();  
    hashSet.add("java001");  
    hashSet.add("java01");  
    hashSet.add("java011");  
    hashSet.add("java002");  
    hashSet.add("java004");  
    

    从HashSet的add()方法可以看出,只有一个参数,并没有【键-值对】。

    其实是HashSet只使用了HashMap的key,value统一是一个固定的Object,因此保证没有重复元素的方法,也是使用的HashMap的key来保证的。

    既然HashSet是使用HashMap来实现的,那么HashSet必然会有一个HashMap的对象,即:

        private transient HashMap<E,Object> map;
    
        // Dummy value to associate with an Object in the backing Map
        private static final Object PRESENT = new Object();
    
        /**
         * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
         * default initial capacity (16) and load factor (0.75).
         */
        public HashSet() {
            map = new HashMap<>();
        }
    

    其中,PRESENT = new Object()是用来作为HashMap的value使用的。可以通过其add()方法看到

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

    在add()方法,添加的元素作为key,PRESENT作为value。

    remove()方法也是调用的HashMap的remove()方法

        /**
         * Removes the specified element from this set if it is present.
         * More formally, removes an element <tt>e</tt> such that
         * <tt>(o==null ? e==null : o.equals(e))</tt>,
         * if this set contains such an element.  Returns <tt>true</tt> if
         * this set contained the element (or equivalently, if this set
         * changed as a result of the call).  (This set will not contain the
         * element once the call returns.)
         *
         * @param o object to be removed from this set, if present
         * @return <tt>true</tt> if the set contained the specified element
         */
        public boolean remove(Object o) {
            return map.remove(o)==PRESENT;
        }
    

      

    同样,size() / isEmpty() / contains() / clear() 都是调用的HashMap相应的方法。

        /**
         * Returns the number of elements in this set (its cardinality).
         *
         * @return the number of elements in this set (its cardinality)
         */
        public int size() {
            return map.size();
        }
    
        /**
         * Returns <tt>true</tt> if this set contains no elements.
         *
         * @return <tt>true</tt> if this set contains no elements
         */
        public boolean isEmpty() {
            return map.isEmpty();
        }
        /**
         * Returns <tt>true</tt> if this set contains the specified element.
         * More formally, returns <tt>true</tt> if and only if this set
         * contains an element <tt>e</tt> such that
         * <tt>(o==null ? e==null : o.equals(e))</tt>.
         *
         * @param o element whose presence in this set is to be tested
         * @return <tt>true</tt> if this set contains the specified element
         */
        public boolean contains(Object o) {
            return map.containsKey(o);
        }
    
        /**
         * Removes all of the elements from this set.
         * The set will be empty after this call returns.
         */
        public void clear() {
            map.clear();
        }
    

      

    另外,HashSet中有个更明显的方法来说明【HashSet只使用了HashMap的key】,即iterator()方法

        /**
         * Returns an iterator over the elements in this set.  The elements
         * are returned in no particular order.
         *
         * @return an Iterator over the elements in this set
         * @see ConcurrentModificationException
         */
        public Iterator<E> iterator() {
            return map.keySet().iterator();
        }
    

      

  • 相关阅读:
    sessionstorage中存储JSON数据
    JS + Canvas画图Demo
    JavaWeb中实现通过邮箱找回密码
    javaWeb实现验证码--代码超简单
    maven的下载、安装及配置
    web端百度地图API实现实时轨迹动态展现
    java存储数据到本地txt文件中
    java读取地址数据文件
    在服务器的tomcat中部署手机apk项目,浏览器或手机下载不能根据URL下载和安装apk文件
    虚拟机类加载机制
  • 原文地址:https://www.cnblogs.com/lixiaolun/p/8808151.html
Copyright © 2020-2023  润新知