国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

JVM里面hashtable和hashmap實現原理

2019-11-14 15:24:49
字體:
來源:轉載
供稿:網友

JVM里面hashtable和hashmap實現原理

 
文章分類:java編程

轉載

 

在hashtable和hashmap是java里面常見的容器類,

是Java.uitl包下面的類,

那么Hashtable和Hashmap是怎么實現hash鍵值對配對的呢,我們看看jdk里面的源碼,分析下Hashtable的構造方法,put(K, V)加入方法和get(Object)方法就大概明白了。

一、Hashtable的構造方法:Hashtable(int initialCapacity, float loadFactor)

    public Hashtable(int initialCapacity, float loadFactor) {
 if (initialCapacity < 0)
     throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal Load: "+loadFactor);

        if (initialCapacity==0)
            initialCapacity = 1;
 this.loadFactor = loadFactor;
 table = new Entry[initialCapacity];
 threshold = (int)(initialCapacity * loadFactor);
    }

這個里面米內有什么好說的,要注意的是table一個是實體數組,輸入的initialCapacity表示這個數組的大小,而threshold 是一個int值,其中loadFactor默認是0.75,就是說明,當table里面的值到75%的閥值后,快要填滿數組了,就會自動雙倍擴大數字容 量。

   而Entry<K,V> 來自與

PRivate static class Entry<K,V> implements Map.Entry<K,V> {
            int hash;
            K key;
            V value;
            Entry<K,V> next;
是一個單項鏈表,

上圖就是Hashtable的表,

二、put(K, V)加入方法

    public synchronized V put(K key, V value) {
 // Make sure the value is not null
 if (value == null) {
     throw new NullPointerException();
 }

 // Makes sure the key is not already in the hashtable.
 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)) {
  V ld = e.value;
  e.value = value;
  return old;
     }
 }

 modCount++;
 if (count >= threshold) {
     // Rehash the table if the threshold is exceeded
     rehash();

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

 // Creates the new entry.
 Entry<K,V> e = tab[index];
 tab[index] = new Entry<K,V>(hash, key, value, e);
 count++;
 return null;
    }

這個看看起來很長,只要注意三點就可以了,

1.index,index是鍵值的hashcode和0x7FFFFFFF的&運算,然后根據數組長度求余值。這樣可以定出所在隊列名稱,

2、

 for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
     if ((e.hash == hash) && e.key.equals(key)) {
  V ld = e.value;
  e.value = value;
  return old;
     }
 }

for語句里面是讓e在tab某個隊列名為index單項鏈表里面遍歷,第幾個單項鏈表的定義由index定義,看看該隊列是否已經有同樣的值,如果有,就返回該值。

3、如果沒有,則加入

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

三、get(Object),獲得

    public synchronized V get(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 e.value;
     }
 }
 return null;
    }

這個就很好理解了 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 e.value;
     }
 }

在該隊里面遍歷,根據hash值獲得具體值。

 

總結下,一個是根據index確定隊列,在由hash值獲得具體元素值。

 

看完了hashtable, 我們在來看看hashMap
hashMap可以算作是hashtable的升級版本, 最早從1.2開始有的. 

但是, 兩者之間最主要的不同有兩點.
1.     hashMap的讀寫是unsynchronized, 在多線程的環境中要注意使用
而hashtable是synchronized
這兩者的不同是通過在讀寫方法上加synchronized關鍵字來實現的.

第二個不同是hashMap可以放空值, 而hashtable就會報錯.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 富宁县| 木兰县| 峨眉山市| 临汾市| 新源县| 兴城市| 防城港市| 汶川县| 方山县| 沁阳市| 宜州市| 当涂县| 高青县| 玉树县| 惠来县| 莲花县| 沂源县| 海城市| 西充县| 石台县| 莲花县| 阳高县| 新竹县| 黄山市| 汝州市| 仪征市| 内丘县| 鹰潭市| 瑞丽市| 静海县| 南昌市| 浙江省| 长岭县| 二连浩特市| 苍南县| 玛纳斯县| 绍兴县| 拉孜县| 漳州市| 秭归县| 册亨县|