zoukankan      html  css  js  c++  java
  • hashtable 和 hashmap 的区别

     }

    区别

    Hashtable

    Hashmap

    继承、实现

    Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable,Serializable

    HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable,Serializable

    线程同步

    已经同步过的可以安全使用

    未同步的,可以使用Colletcions进行同步Map Collections.synchronizedMap(Map m)

    对null的处理

     

    Hashtable table = new Hashtable();

    table.put(null, "Null");

    table.put("Null", null);

    table.contains(null);

    table.containsKey(null);

    table.containsValue(null);

    后面的5句话在编译的时候不会有异常,可在运行的时候会报空指针异常具体原因可以查看源代码

    public synchronized V put(K key, V value) {

    // Make sure the value is not null

    if (value == null) {

    throw new NullPointerException();

    }

    HashMap map = new HashMap();
    map.put(null, "Null");

    map.put("Null", null);

    map.containsKey(null);

    map.containsValue(null);

    以上这5条语句无论在编译期,还是在运行期都是没有错误的.

    在HashMap中,null可以作为键,这样的键只有一个;可以有一个或多个键所对应的值为null。当get()方法返回null值时,即可以表示 HashMap中没有该键,也可以表示该键所对应的值为null。因此,在HashMap中不能由get()方法来判断HashMap中是否存在某个键,而应该用containsKey()方法来判断。

    增长率

    protected void rehash() {

    int oldCapacity = table.length;

    Entry[] oldMap = table;

    int newCapacity = oldCapacity * 2 + 1;

    Entry[] newMap = new Entry[newCapacity];

    modCount++;

    threshold = (int)(newCapacity * loadFactor);

    table = newMap;

    for (int i = oldCapacity ; i-- > 0 ;) {

    for (Entry<K,V> old = oldMap[i] ; old != null ; ) {

    Entry<K,V> e = old;

    old = old.next;

    int index = (e.hash & 0x7FFFFFFF) % newCapacity;

    e.next = newMap[index];

    newMap[index] = e;

    }

    }

    }

     

    void addEntry(int hash, K key, V value, int bucketIndex) {

    Entry<K,V> e = table[bucketIndex];

    table[bucketIndex] = new Entry<K,V>(hash, key, value, e);

    if (size++ >= threshold)

    resize(2 * table.length);

    }

     

    哈希值的使用

    HashTable直接使用对象的hashCode,代码是这样的:

    public synchronized boolean containsKey(Object key) {

    Entry tab[] = table;

    int hash = key.hashCode();

    int index = (hash & 0x7FFFFFFF) % tab.length;

    for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {

    if ((e.hash == hash) && e.key.equals(key)) {

    return true;

    }

    }

    return false;

    }

    HashMap重新计算hash值,而且用与代替求模

    public boolean containsKey(Object key) {

    Object k = maskNull(key);

    int hash = hash(k.hashCode());

    int i = indexFor(hash, table.length);

    Entry e = table[i];

    while (e != null) {

    if (e.hash == hash && eq(k, e.key))

    return true;

    e = e.next;

    }

    return false;

    }

  • 相关阅读:
    关于loose.dtd和xhtml1transitional.dtd等文档类型定义模型中CSS失效的解决办法。
    JSON扫盲帖+JSON类教程
    jQuery中Ajax事件
    JQuery绑定事件 时如何传递参数
    xml include 另外一个xml文件
    ubuntu 两张网卡时网络设置
    Letcode 题:pow(x,n)
    Java编程语言中sleep()和yield()的区别
    JProfiler与eclipse集成
    zz 字符串相关
  • 原文地址:https://www.cnblogs.com/chenyao/p/3134355.html
Copyright © 2011-2022 走看看