HashSet Implementation principle

Source: Internet
Author: User

1. HashSet Overview

HashSet implements the set interface, which is supported by a hash table (actually a hashmap instance). It does not guarantee the set's iteration order, especially it does not guarantee that the order is constant. This class allows the use of NULL elements.

2. Implementation of HashSet

If not, it is added to the linked list corresponding to the array index.

------------------------------------------------------------------------------------------

Set implementation class can not have duplicate elements in the collection object, HashSet is the same as he used an identity to determine the elements of the non-repetition, hashset with an algorithm to ensure that hashset elements are not duplicated, hashset using a hashing algorithm, the underlying array to store data. Default initialization capacity 16, load factor 0.75

The method of Hashcode () in the object class is that all subclasses inherit this method, and this method calculates a hash (hash) value return using the hash algorithm, and the hashset will use the hash code value to take the modulus of the array length. Modulo (where the object is to be placed in the array) determines whether the elements in the array are the same as the contents of the objects to be joined, and if they are different, they are added.

Hash algorithm is a hashing algorithm.

Set hs=new HashSet ();

Hs.add (o);
|
O.hashcode ();
|
o% Current Total Capacity (0--15)
|
| No conflicts
If there is a conflict-----------------direct storage
|
| Conflict occurred
| False (Not equal)
O1.equals (O2)-------------------Find a vacancy to add
|
| Yes (equal)
Do not add

The principle of overriding the Hashcode () method:
1. Be sure to return the same hashcode value to the objects that we think are the same
2, try to let those we think that different objects return different hashcode values, otherwise, will increase the probability of conflict.
3, as far as possible to let Hashcode value Liekai (two value with a different or operation can make the result range wider)

The implementation of HashSet is relatively simple, the related HashSet operations are basically directly called the underlying hashmap of the relevant methods to complete, we should save to the object in HashSet hashcode () and Equals (), Since the object is added to the HashSet, the Hashcode method is called first to calculate the hash value of the object, and then the hash method in the HashMap is called according to the hash value, the resulting value & (LENGTH-1) Gets the index of the save position of the object in the HashMap transient entry[] table, and then finds the object that is saved in the array at that index position, and calls the Equals method to compare whether the two objects are equal or not, if the equality is not added. Note: Therefore, the custom class in the collection object to be deposited in hashset must overwrite the hashcode (), Equals () two methods to ensure that the elements in the collection are not duplicated. When overriding the Equals () and Hashcode () methods, the Hashcode () method of the same object returns the same value, overriding the Equals () method to determine its contents. In order to ensure the efficiency, so when covering the Hashcode () method, also try to make different objects as far as possible to return different hash code values.

If the elements in the array and the hashcode () of the object to be joined return the same hash value (the same object), the Equals () method is used to determine whether the contents of the two objects are the same.

------------------------------------------------------------------------------------------

The source code for HashSet is as follows:

[Java]View PlainCopy
    1. Public class Hashset<e>
    2. Extends abstractset<e>
    3. Implements Set<e>, Cloneable, java.io.Serializable
    4. {
    5. Static final long serialversionuid = -5024744406713321676l;
    6. The underlying uses HashMap to hold all the elements in the hashset.
    7. Private transient hashmap<e,object> map;
    8. Defines a dummy object as the value of HashMap, which defines this object as static final.
    9. Private static final Object PRESENT = new Object ();
    10. /**
    11. * The default parameterless constructor constructs an empty hashset.
    12. *
    13. * The actual underlying will initialize an empty HashMap and use the default initial capacity of 16 and load factor 0.75.
    14. */
    15. Public HashSet () {
    16. Map = new hashmap<e,object> ();
    17. }
    18. /**
    19. * Constructs a new set containing the elements in the specified collection.
    20. *
    21. * The actual underlying uses the default load factor of 0.75 and is sufficient to contain the specified
    22. * The initial capacity of all elements in collection to create a hashmap.
    23. * @param c where the elements will be stored in the collection in this set.
    24. */
    25. Public HashSet (COLLECTION<? extends e> c) {  
    26. Map = New Hashmap<e,object> (Math.max ((int) (C.size ()/.75f) + 1, 16));
    27. AddAll (c);
    28. }
    29. /**
    30. * Constructs an empty hashset with the specified initialcapacity and loadfactor.
    31. *
    32. * The actual bottom layer constructs an empty hashmap with corresponding parameters.
    33. * @param initialcapacity initial capacity.
    34. * @param loadfactor loading factor.
    35. */
    36. Public HashSet (int initialcapacity, float loadfactor) {
    37. Map = New Hashmap<e,object> (initialcapacity, loadfactor);
    38. }
    39. /**
    40. * Constructs an empty hashset with the specified initialcapacity.
    41. *
    42. * The actual bottom layer constructs an empty hashmap with corresponding parameters and load factor loadfactor of 0.75.
    43. * @param initialcapacity initial capacity.
    44. */
    45. Public HashSet (int initialcapacity) {
    46. Map = new hashmap<e,object> (initialcapacity);
    47. }
    48. /**
    49. * Constructs a new empty-link hash collection with the specified initialcapacity and loadfactor.
    50. * This constructor is for package access, not public, and is actually just a support for linkedhashset.
    51. *
    52. * The actual underlying will be implemented with an empty Linkedhashmap instance constructed with the specified parameters.
    53. * @param initialcapacity initial capacity.
    54. * @param loadfactor loading factor.
    55. * @param dummy mark.
    56. */
    57. HashSet (int initialcapacity, float loadfactor, boolean dummy) {
    58. Map = New Linkedhashmap<e,object> (initialcapacity, loadfactor);
    59. }
    60. /**
    61. * Returns an iterator that iterates over the elements in this set. The order of the returned elements is not specific.
    62. *
    63. * The underlying actual call to the underlying HashMap keyset to return all keys.
    64. * Visible hashset elements, just stored in the underlying HashMap key,
    65. * Value uses a static final object identifier.
    66. * The iterator that iterates over the elements in this set @return.
    67. */
    68. Public iterator<e> Iterator () {
    69. Return Map.keyset (). iterator ();
    70. }
    71. /**
    72. * Returns the number of elements in this set (capacity of Set).
    73. *
    74. * The size () method of the underlying actual call to HashMap returns the number of entry, and the number of elements in the set is obtained.
    75. * @return The number of elements in this set (capacity of Set).
    76. */
    77. public int size () {
    78. return Map.size ();
    79. }
    80. /**
    81. * Returns True if this set contains no elements.
    82. *
    83. * The underlying actual call to HashMap IsEmpty () determines whether the hashset is empty.
    84. * @return Returns True if this set contains no elements.
    85. */
    86. Public Boolean IsEmpty () {
    87. return Map.isempty ();
    88. }
    89. /**
    90. * Returns True if this set contains the specified element.
    91. * More precisely, when and only if this set contains a satisfying (o==null? E==null:o.equals (e))
    92. * When the E element returns true.
    93. *
    94. * The underlying actual call to HashMap ContainsKey determines whether the specified key is included.
    95. * @param o The existence of the element that has been tested in this set.
    96. * @return Returns True if this set contains the specified element.
    97. */
    98. Public Boolean contains (Object o) {
    99. return Map.containskey (o);
    100. }
    101. /**
    102. * If the specified element is not already contained in this set, the specified element is added.
    103. * More specifically, if this set does not contain the content (E==null? E2==null:e.equals (E2))
    104. * element E2, the specified element e is added to this set.
    105. * If this set already contains the element, the call does not change the set and returns false.
    106. *
    107. * The underlying actually puts the element as key into the HashMap.
    108. * Because the put () method of HashMap adds Key-value pair, when the new is placed in the entry of HashMap key
    109. * Same as key with entry in the Set (Hashcode () return value is equal, also return true by equals),
    110. * The value of the newly added entry will overwrite the value of the original entry, but there will be no change to key.
    111. * Therefore, if an existing element is added to HashSet, the newly added collection element will not be placed in the HashMap,
    112. * The original element will not have any changes, which satisfies the feature that the element in set does not repeat.
    113. * @param e is added to the element in this set.
    114. * @return Returns True if this set does not already contain the specified element.
    115. */
    116. Public Boolean Add (E e) {
    117. Return Map.put (E, PRESENT) = =null;
    118. }
    119. /**
    120. * If the specified element is present in this set, it is removed.
    121. * More specifically, if this set contains an element e that satisfies (o==null. E==null:o.equals (e)),
    122. * Remove it. Returns True if this set already contains the element
    123. * (or: Returns True if this set changes because of a call). (Once the call returns, the set no longer contains the element).
    124. *
    125. * The lower layer actually calls HashMap's Remove method to delete the specified entry.
    126. * @param o An object that needs to be removed if it exists in this set.
    127. * @return Returns True if the set contains the specified element.
    128. */
    129. Public boolean remove (Object o) {
    130. return Map.Remove (o) ==present;
    131. }
    132. /**
    133. * Removes all elements from this set. When this call returns, the set will be empty.
    134. *
    135. * The lower layer actually calls HashMap's clear method to empty all elements in the entry.
    136. */
    137. Public void Clear () {
    138. Map.clear ();
    139. }
    140. /**
    141. * Returns a shallow copy of this HashSet instance: The elements themselves are not copied.
    142. *
    143. * The underlying actually calls HashMap's Clone () method, gets the shallow copy of the HashMap, and sets it to HashSet.
    144. */
    145. Public Object Clone () {
    146. try {
    147. hashset<e> Newset = (hashset<e>) super.clone ();
    148. Newset.map = (hashmap<e, object>) Map.clone ();
    149. return newset;
    150. } catch (Clonenotsupportedexception e) {
    151. Throw new Internalerror ();
    152. }
    153. }
    154. }

HashSet Implementation principle

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.