First speaking Set
An overview
Set:1. Element Storage has no subscript, so elements are unordered ( the order in which they are deposited and fetched is not necessarily consistent
2. Elements cannot be duplicated
|--hashset: The underlying data structure is a hash table . Thread is out of sync. The principle of guaranteeing element uniqueness is to determine whether the hashcode value of an element is the same. If it is the same, it continues to judge whether the element's Equals method is true.
|--treeset: You can sort the elements in the set collection. The default is to sort alphabetically by nature. The underlying data structure is a two-fork tree. The basis for guaranteeing the uniqueness of the element: CompareTo method return 0.
The set set has no unique functionality, and the function of the set set is consistent with the collection
Two HashSet
HashSet: Thread is unsafe, access speed is fast.
HashSet principle: HashSet can not be stored in duplicate values, if Add () stored in duplicate values, return false, then hashset how to ensure that the data is not unique?
The hashing algorithm increases the efficiency of de-duplication and reduces the number of uses of the Equals () method if it is less efficient when compared to equals () at the time of storage.
Element uniqueness can be ensured through the two methods of the element, hashcode and equals. If the hashcode value of the element is the same, the equals is not determined to be true. If an element has a different hashcode value, equals is not called.
2. After knowing the hashset principle, we must override the Hashcode () and Equals () methods when storing custom classes
* Hashcode (): The return value of the same object must be the same, the return value of the property is different (improve efficiency)
* Equals (): property returns True, different property returns False, returns False when storing
Java Basics-Set of 2Set interfaces and map interfaces