zoukankan      html  css  js  c++  java
  • HashMap与Hashtable的区别转

    本文转自:http://oznyang.iteye.com/blog/30690 并做少许修改

    HashTable的应用非常广泛,HashMap是新框架中用来代替HashTable的类,也就是说建议使用HashMap,不要使用HashTable。可能你觉得HashTable很好用,为什么不用呢?这里简单分析他们的区别。 
    1.HashTable的方法是同步的,HashMap未经同步,所以在多线程场合要手动同步HashMap这个区别就像Vector和ArrayList一样。

    2.HashTable不允许null值(key和value都不可以),HashMap允许null值(key和value都可以)。

    3.HashTable有一个contains(Object value),功能和containsValue(Object value)功能一样。

    4.HashTable使用Enumeration,HashMap使用Iterator。

    以上只是表面的不同,它们的实现也有很大的不同。

    5.HashTable中hash数组默认大小是11,增加的方式是 old*2+1。

    View Code
    API源码如下:
    
    初始大小为11
     /**
         * Constructs a new, empty hashtable with a default initial capacity (11)
         * and load factor (0.75).
         */
        public Hashtable() {
        this(11, 0.75f);
        }
    
    默认大小和自增方式:
     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;
            }
        }
        }

    HashMap中hash数组的默认大小是16,而且一定是2的指数。

    View Code
    API源码如下:
    
    初始大小是16:
    
    static final int DEFAULT_INITIAL_CAPACITY = 16;
    
    
    自增方式:
    
     void resize(int newCapacity) {
            Entry[] oldTable = table;
            int oldCapacity = oldTable.length;
            if (oldCapacity == MAXIMUM_CAPACITY) {
                threshold = Integer.MAX_VALUE;
                return;
            }
    
            Entry[] newTable = new Entry[newCapacity];
            transfer(newTable);
            table = newTable;
            threshold = (int)(newCapacity * loadFactor);
        }

     
    6.哈希值的使用不同,HashTable直接使用对象的hashCode,代码是这样的:

    View Code
    int hash = key.hashCode();
    int index = (hash & 0x7FFFFFFF) % tab.length;


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

    View Code
    int hash = hash(k);
    int i = indexFor(hash, table.length);
    
    static int hash(Object x) {
      int h = x.hashCode();
    
      h += ~(h << 9);
      h ^= (h >>> 14);
      h += (h << 4);
      h ^= (h >>> 10);
      return h;
    }
    static int indexFor(int h, int length) {
      return h & (length-1);
    }


    以上只是一些比较突出的区别,当然他们的实现上还是有很多不同的,比如
    HashMap对null的操作

  • 相关阅读:
    proc_create和create_proc_entry的区别
    ubuntn 12.04 rk环境及 相关使用 配置
    postcore_initcall(), arch_initcall(), subsys_initcall(), device_initcall() 调用顺序
    linux的zip 命令
    Mutex::AutoLock介绍
    camera 管脚功能和调试分析
    Android设备中实现Orientation Sensor(图)兼谈陀螺仪
    struct stat 操作 小结
    linux中字符串转换函数 simple_strtoul
    吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:HibernateMap集合属性
  • 原文地址:https://www.cnblogs.com/wangchy0927/p/2830649.html
Copyright © 2011-2022 走看看