Java record-70-HashSet source code profiling

Source: Internet
Author: User

Java record-70-HashSet source code profiling
HashSet does not contain duplicate elements, which are supported by the hash table. Next I will start to analyze the source code of the HashSet: first, start with the constructor;

    private transient HashMap<E,Object> map;    public HashSet() {        map = new HashMap<E,Object>();    }    public HashSet(Collection<? extends E> c) {        map = new HashMap<E,Object>(Math.max((int) (c.size()/.75f) + 1, 16));    addAll(c);    }    public HashSet(int initialCapacity, float loadFactor) {        map = new HashMap<E,Object>(initialCapacity, loadFactor);    }    public HashSet(int initialCapacity) {        map = new HashMap<E,Object>(initialCapacity);    }

 

We will be surprised to find that the HashMap constructor of HashSet has constructed HashMap and directly uses HashMap. But HashMap is key-value ing, and HashSet is stored in objects. Is this all possible? With this question, let's look at the following:
    // Dummy value to associate with an Object in the backing Map    private static final Object PRESENT = new Object();         public Iterator<E> iterator() {        return map.keySet().iterator();    }         public int size() {        return map.size();    }         public boolean isEmpty() {        return map.isEmpty();    }    public boolean contains(Object o) {        return map.containsKey(o);    }    public boolean add(E e) {        return map.put(e, PRESENT)==null;    }    public boolean remove(Object o) {        return map.remove(o)==PRESENT;    }    public void clear() {        map.clear();    }

 

From the above method, we can see that HashSet is a shell, which is implemented by calling the HashMap method internally. The add method adds the elements to be added as the key of the HashMap. The key of the HashMap cannot be repeated, so that the elements of the HashSet will not be repeated. So what is the value of HashMap stored? PRESENT, a static, Object-type variable. The key in this HashMap stores the elements to be stored in the HashSet, while the value stores a unified static Object variable. Why does this PRESENT need static final? Save space. In this case, the implementation of HashSet is simple. HashMap is fully used for construction and use, and all implementations are handed over to HashMap. How is HashMap implemented? Let's look forward to analyzing the source code of HashMap.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.