Java (ii) ----- HashSet

Source: Internet
Author: User

HashSet <E> AbstractSet <E> Set <E>, Cloneable, java. io. Serializable

HashSet inherits the AbstractSet class and implements the Set, Cloneable, and Serializable interfaces. AbstractSet provides the backbone implementation of the Set interface to minimize the work required to implement this interface. The Set interface is a Collection that does not include repeated elements. It maintains its own internal sorting, so random access makes no sense.

Basic Attributes

          HashMap<E,Object>           Object PRESENT =  Object();

Constructor

        =  HashMap<>         HashSet(Collection<?  E>=  HashMap<>(Math.max(() (c.size()/.75f) + 1, 16         HashSet( initialCapacity, =  HashMap<>         HashSet(=  HashMap<> initialCapacity,  loadFactor, =  LinkedHashMap<>

From the constructor, we can see that all the constructor of HashSet constructs a new HashMap. The last constructor is not disclosed for the packet access permission, it takes effect only when hashset is used.

Ii. Method

Since HashSet is based on HashMap, the implementation process of its method is very simple for HashSet.

 Iterator<E>

The iterator () method returns the iterator for iteration of the elements in this set. The returned element sequence is not specific. The underlying keySet that calls HashMap returns all keys. This reflects that all elements in HashSet are stored in the key of HashMap, and value is the PRESENT object used, and the object is static final.

 

Size () returns the number of elements in the set (set capacity ). The size method of HashMap is called at the underlying layer to return the size of the HashMap container.

 

IsEmpty () to determine whether the HashSet () set is null. If it is null, returnTrue. Otherwise, false is returned..

 

Contains () to determine whether an element exists in HashSet (). If yes, true is returned. Otherwise, false is returned. To be more precise, true can be returned only when this relationship is satisfied: (o = null? E = null: o. equals (e )). The underlying layer calls containsKey to determine whether the key value of HashMap is null.

  map.put(e, PRESENT)==

Add () if the set does not contain the specified element, add the specified element. If this Set does not contain meet (e = null? E2 = null: e. equals (e2) e2, e2 is added to the Set. Otherwise, false is returned if no value is added. Because the bottom layer uses the put Method of HashMap to construct key = e, value = PRESENT into key-value pairs, when this e exists in the key of HashMap, the value will overwrite the original value, but the key will remain unchanged. Therefore, if you add an existing Eelement to the HashSet, the newly added element will not be saved to the HashMap, therefore, this satisfies the unique features of elements in a HashSet.

  map.remove(o)==

Remove: if the specified element exists in this set, remove it. The underlying layer uses the remove Method of HashMap to delete the specified Entry.

 

Clear removes all elements from this set. The bottom layer calls the clear method of HashMap to clear all entries.

<E> newSet = (HashSet<E>) = (HashMap<E, Object> 

Clone returns thisHashSetInstance superficial copy: these elements are not copied.

Postscript:

Because HashSet uses HashMap implementation at the underlying layer, the implementation process becomes very simple. If you have a better understanding of HashMap, HashSet is simply a piece of cake. There are two methods that are very important for HashMap and HashSet. The following sections will detail hashcode and equals.

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.