The principle of HashSet, easy to understand

HashSet is actually a HashMap instance, is a linked list of storage arrays. It does not guarantee the order of iterations storage elements; class allows null elements. HashSet elements are not allowed in duplicate, because HashSet is, the elements HashSet HashMap based implementation of the key are stored in a HashMap above, the value of the value of the object is a uniform fixed private static final Object PRESENT = new Object ();

HashSet add in the method call is the bottom of HashMap put () method, and if call put in a HashMap, first determines whether key exists, if the key exists modifying value value, if the key does not exist that is inserted into the key-value . In this set, since the value of no use value, there would be no modification of the value argument value, thus adding to the HashSet element, the element is first determined (i.e. key) exists, if this insert is not present, if there is not inserted , so that there is no duplicate values ​​in HashSet.

 It is determined whether key exists equals the class will override the elements () and hashCode () method, when an object is added to the Set, the first call to hashCode () method to calculate the hash value of the second object where the class of this object, this hash value determines the position of the object stored in the Set; if this position is not directly stored in the storage object Ruoyi through a subject in the class where the object equals () compares two objects are the same, the same can not be Add to.

Guess you like

Origin www.cnblogs.com/linliquan/p/11579013.html