Java-HashMap analysis, javahashmap
(1) Hash Algorithm
Hash algorithm, which maps the original data to a shorter, fixed-length binary value through the hash function.
The hash algorithm has two basic features: repeatable and irreversible. The hash value calculated theoretically can be repeated, but this is basically not the case for good hash functions. Irreversible means that the hash value cannot be used to calculate the original data.
Hash algorithms are generally used for fast search (such as HashMap) and encryption algorithms (such as MD5 ).
(2) hashcode in java
In java, the Object base class Object has the hashcode method. native calls the default return Object memory address. hashcode returns an indefinite 10-digit number.
Java specifies the eqauls and hashCode methods as follows: 1. If the two objects are the same (equal), their hashCode values must be the same; 2. If the two objects have the same hashCode, they are not necessarily the same.
Hashcode can be rewritten, while String can be rewritten.
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]
The int algorithm is used. Here, s [I] is the I character of the string, n is the length of the string, and ^ represents the power. (The hash code of the Null String is 0 .)
(3) hashmap source code analysis
Data format: a zipper hash table combining arrays and linked lists.
/*** Basic data structure of the entire HashMap */transient HashMapEntry <K, V> [] table; static class HashMapEntry <K, V> implements Entry <K, v> {// key final K key in put (key, value); // value V value in put (key, value; // The hash value final int hash calculated by the hash ing function; // points to the next 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 excessive conflicts (collisions) caused by poor Hash Functions) problem, use the ing function to generate the int hash = hash (key. hashCode (); // calculates the array position coordinate of the hash. int I = indexFor (hash, table. length); // check whether the key already exists in the for (Entry <K, V> e = table [I]; e! = Null; e = e. next) {Object k; // the 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 addEntry (hash, key, value, I) from the header; return null ;}
(4) differences between HashMap and HashTable
1. Different inheritance
Hashtable extends Dictionary
HashMap extends AbstractMap
2. Synchronization
HashMap is non-synchronous, and HashTable is synchronous
3. insert value
The HashMap key value can be null, and HashTable cannot.
When HashMap obtains null through the get method, it does not mean that the key does not exist. It must be determined 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
How to use JAVA HASHMAP
Import java. util .*;
Public class Test {
Public static void main (String [] args ){
HashMap map = new HashMap ();
System. out. println ("the current map has" + map. size () + "elements ");
Map. put ("Study Committee member", "zhangsan ");
Map. put ("Life Committee member", "lisi ");
Map. put ("Sports Committee member", "wangwu ");
System. out. println ("the current map has" + map. size () + "elements ");
System. out. println ("this class's Life committee member is:" + map. get ("Life Committee member "));
Map. remove ("Life Member ");
System. out. println ("the current map has" + map. size () + "elements ");
}
}
Use HashMap in java
String