容器-Map-Hashtable

          1.Hashtable概要:實現Map接口的同步實現
          •     線程安全
          •     不能存儲null到key和value
          •    HashTable中hash數組默認大小是11,增加的方式是 old*2+1。HashMap中hash數組的默認大小是16,而且一定是2的指數

            區別

            Hashtable

            Hashmap

            繼承、實現

            Hashtable extends Dictionaryimplements Map, Cloneable,Serializable

            HashMap extends AbstractMap implements Map, 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 old = oldMap[i] ; old != null ; ) {

            Entry 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 e = table[bucketIndex];

            table[bucketIndex] = new Entry(hash, key, value, e);

            if (size++ >= threshold)

            resize(2 * table.length);

            }

            哈希值的使用

            HashTable直接使用對象的hashCode,代碼是這樣的:

            public synchronized booleancontainsKey(Object key) {

            Entry tab[] = table;

            int hash = key.hashCode();

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

            for (Entry 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;

            }

          posted on 2012-02-21 10:32 陳睿 閱讀(1137) 評論(0)  編輯  收藏 所屬分類: 高級

          導航

          <2012年2月>
          2930311234
          567891011
          12131415161718
          19202122232425
          26272829123
          45678910

          統計

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 芮城县| 岐山县| 潢川县| 涞源县| 盘锦市| 呼玛县| 桓仁| 铅山县| 惠来县| 买车| 上饶市| 新邵县| 木里| 麦盖提县| 兴国县| 弥勒县| 蓝山县| 南宁市| 黎平县| 巫山县| 临湘市| 宝清县| 高阳县| 喜德县| 奉新县| 漳浦县| 乐亭县| 礼泉县| 庆元县| 库车县| 红安县| 宜黄县| 商洛市| 九江市| 尉犁县| 彩票| 长泰县| 安义县| 顺昌县| 温州市| 红河县|