Monday, February 29, 2016

[Java] HashMap et HashTable

HashMap VS HashTable

Inherits:

public class Hashtable<K,V>  
                                             extends Dictionary<K,V>  
                                             implements Map<K,V>, Cloneablejava.io.Serializable
public class HashMap  extends AbstractMap implements Map

 Synchronization :

HashTable is synchronized,  can be used in multi-threading
HashMap is not, should manage synchronization in multi-threading

null key ? null value?:


HashTable do not allow null value or key
HashMap can have several null values and one null key

hash:

HashTable uses directly  the hashCode of an object : 
public synchronized V put(K key, V value) {  
...
        int hash = hash(key); 

....
}

private int hash(Object k) {  
        return hashSeed ^ k.hashCode();  
    } 


HashMap recalcule the hash value.
First use hash to check if same, if hash same, use "equals()" to check
hash(key.hashCode()); 
 

Expansion : (扩容)

 HashTable 11 by default, old*2+1 expansion
HashMap 16 by default, n^2 expansion : 16, 32,64.....


both use linked list for collisions

No comments:

Post a Comment