How to use hash and hash
Hash (hash )?
Hash data structure, but java is shielded here. It is encapsulated into HashSet, HashMap, and HashTable in the form of encapsulation. HashTable is out of date.
Hash Algorithm: The hashCode () function in java and Its rewriting.
Purpose: generate a unique identifier for each object.
Generates a unique hashCode for each object based on its characteristics, uses this hashCode as its subscript, and saves the object value as an element value to an array (Object reference ). at this time, this array can be seen as an associated array. java does not have the concept of correlated arrays. A separate name is HashSet.
Hash process:
Obtain the object, call its own hash algorithm (hashCode () method), generate a unique hash value, and save the value to the array. The whole process is hash. the relationship between an array and a unique value and an object is HashSet;
In java, the hash algorithm refers to the algorithm in the hashCode () function of the Object, which is defined according to the class you write.
Hash conflict: Multiple objects may generate the same hash value.
Why is the HashSet unordered and repeatable?
1. The generated hashCode does not have a certain size relationship, so it does not matter the order.
2. Each object must generate a unique hashCode (multiple objects can generate the same but compare values). If it is a duplicate object, it must generate the same hashCode, and the value is the same. at this time, there is no way to query a specific data. so it must be unique. all is the unique identifier of the data.
HashSet and HashMap
1. HashSet is an implementation of HashMap, essentially HashMap. The data structure of HashMap is a Hash table.
2. A Hash table is also called a Hash table. Its underlying layer is an array composed of hashCode () functions. Each array element is a one-way linked list. each single linked list has a unique hash value, which is the subscript of the array. It also means that the hash value of each single linked list node is the same.
How to add elements to a Hash table.
1) when you want to add a Map, HashMap stores the ing relationship. What is the maintenance of this ing relationship (Map. entry (K, V) is an interface, so we can pass in the ing relationships of various objects. what if duplicate hash values are generated? The system adds a one-way linked list to the HashTable bucket. Each object in the one-way linked list is an Entry.
Key points: 2) Add process: Step 1: First call the key object of the ing relationship to be stored, then call the hashCode () method of the key object to generate the hash code, add elements to the array. without this hash code, it occupies an array space and stores the key-value ing object Entry. if the hash code already exists, perform step 2.
Step 2: equals (), compare the key values in the linked list with the same hash code. If the return value is true, it indicates that the key value already exists in the linked list, if no value is added, false is returned, and step 3 is executed.
Step 3: Move the currently saved node in the array one bit backward (not actually moving, but I just get his memory address, save it in the nextNode of the newly added element), and then save the current ing object Entry to the array.
3) The initial capacity of HashSet and Hashmap is 16, and the default load factor is 0.75.
Index Array: the memory space is continuous and continuous, and the overall data is ordered.
Associated array: the memory space is continuous, and the records are not consecutive or unordered.
Hash Table: an associated array encoded using the hashCode () function.
The hashCode () function we use may be repeated, but the array does not need to be repeated. We use equals () to compare another external attribute (which must contain two comparisons and one compares hashCode, another property is compared .)
HashSet: only one element is saved.
HashMap: stores the ing between two objects in an element, and encapsulates the ing into an object.