• 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();
        }
    

      

  • 相关阅读:
    第0次作业
    第4次作业
    第3次作业
    第2次作业
    C#浮点数保留位数
    第0次作业
    软件工程第4次作业
    软件工程第3次作业
    软件工程第2次作业
    软件工程第1次作业
  • 原文地址:https://www.cnblogs.com/lixiaolun/p/8808151.html
Copyright © 2020-2023  润新知