Java Records -70-hashset Source analysis

Source: Internet
Author: User

HashSet does not contain duplicate elements, which are supported by a hash table.

Below I began to analyze the source code of HashSet:

First, starting from the construction method;

    private transient HashMap<E,Object> map;     Public hashset ()  {        map = new hashmap <E,Object> ();     }    public hashset (collection<?  EXTENDS&NBSP;E&GT;&NBSP;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 HashSet's constructor has been constructed HashMap, using HashMap directly. But HashMap is a key-value mapping, HashSet is a storage object, is this OK? With this problem, let's look down:

    // 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 series of methods can be seen, hashset is a shell Ah, the interior is called the HashMap method to achieve.

The Add method is to add the element as HashMap Key added, HashMap key can not be repeated, so hashset elements will not repeat, brilliant ah. So what does the value of HashMap save? PRESENT, a static, object-type variable. The key in this hashmap holds the element that HashSet wants to store, and value stores a uniform static object variable. Why would this present be static and final? Save space for a chant.

So it seems that the implementation of HashSet is very simple ah, completely borrowed hashmap to construct the use, all the implementation has been given to the HashMap. How is HashMap realized? Let's look at the anatomy of the HashMap source code.

Java Records -70-hashset Source analysis

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.