(i) Hashing algorithm
A hashing algorithm that maps raw data through a hash function to a shorter fixed-length binary value, referred to as a hash value.
There are two basic features of the hash algorithm: repeatable and irreversible. Theoretically computed hashes are repeatable, but a good hash function basically does not happen. cannot be reversed, knowing that the hash value cannot be inferred from the original data.
Hashing algorithms are typically used for quick lookups (such as HASHMAP) and cryptographic algorithms (such as MD5).
(ii) Hashcode in Java
The object base class in Java has the Hashcode method, the native call returns the object memory address by default, and Hashcode returns an indeterminate 10 binary number.
Java for the Eqauls method and Hashcode method is this: 1, if two objects are the same (equal), then their hashcode value must be the same, 2, if two objects hashcode the same, they are not necessarily the same.
Hashcode can be rewritten, string overrides Hashcode.
public int hashcode () {int h = hash; int len = count;if (H = = 0 && len > 0) { int off = offset; Char val[] = value; for (int i = 0; i < len; i++) { h = 31*h + val[off++]; } hash = h; } return h; }
s[0]*31^ (n-1) + s[1]*31^ (n-2) + ... + s[n-1]
Using the int algorithm, where s[i] is the I-character of a string, n is the length of the string, and ^ denotes exponentiation. (The hash code for an empty string is 0.) )
(iii) HASHMAP source analysis
Data format: Arrays and linked lists are combined with a zip method hash table.
/** * The basic data structure of the entire HashMap */ transient hashmapentry<k, v>[] table; Static Class Hashmapentry<k, V> implements Entry<k, key final K key in v> {//put (key,value); Value V value in put (Key,value) ; The hash value is computed by the hashing function, the final int hash; Point to Next Hashmapentry hashmapentry<k, v> next; Hashmapentry (K key, V value, int hash, hashmapentry<k, v> next) { this.key = key; This.value = value; This.hash = hash; This.next = next; } .... }
Public V put (K key, V value) { if (key = = null) return Putfornullkey (value); Prevents poor-quality hash functions from causing excessive collisions (collisions), and further uses mapping functions to generate values int hash = hash (Key.hashcode ()); calculates which array position the hash belongs to, int i = indexfor (hash, table.length); Detects if the key already exists in the linked list of array I for (entry<k,v> e = table[i]; E! = null; e = e.next) { Object K; Hash same, key is not necessarily the same if (E.hash = = Hash && (k = e.key) = = Key | | key.equals (k)) { V oldValue = e.value;
e.value = value; E.recordaccess (this); return oldValue; } } modcount++; Insert the Hashmapentry object from the head addentry (hash, key, value, I); return null; }
Four The difference between HashMap and Hashtable
1. Inherit different
extends Dictionary
HashMap extends abstractmap
2. Synchronization
HashMap is non-synchronous, Hashtable is synchronous
3. Inserting values
HashMap key value allowed for null,hashtable not allowed
HashMap obtained by the Get method to null does not mean that the key does not exist and must be judged by the ContainsKey () method.
hashmap<string, string> HashMap = new hashmap<string, string> (); Hashmap.put ("1", null); System.out.println (Hashmap.get ("1")),//null System.out.println (Hashmap.get ("2"));//null
Java-hashmap Analysis