《java編程思想》:散列的原理

來源:互聯網
上載者:User

標籤:索引值   java編程思想   size   bucket   rri   lis   建立   boolean   style   

以實現一個簡單的HashMap為例,詳細講解在code之中。

簡單解釋散列原理:

1.map中內建固定大小數組,但是數組並不儲存key值本身,而是儲存標識key的資訊

2.通過key產生數組角標,對應位置存放LinkedList,list中存放的是索引值對

3.如此,無論放入多少個索引值對,數組大小都不必改變,當key值產生的角標值重複時,擷取對應位置的list,向list中添加索引值對即可

4.當調用get()方法時,只需遍曆對應角標位置的list,而不用遍曆所有的索引值對資訊,所以加快了查詢速度。

5.注意,get()和put()中使用的計算散列值,也就是數組角標的公式一定要一致,保證計算所得的角標一致

/** * Created by Don9 on 2017/ */public class MyHashMap<K,V> extends AbstractMap<K,V>{    /* 1.自訂數組大小 */    static final int SIZE = 999;    /* 2.建立內部數組,數組存放的是LinkedList,而list中存放的是想要存放的索引值對 */    LinkedList<MyEntry<K,V>>[] buckets = new LinkedList[999];    /* 3.put方法,此方法返回key對應的曾經的oldValue */    public V put(K key,V value){        /* 4.先定義一個傳回值 */        V oldValue = null;        /* 5.根據key計算出一個散列值,用於當作內建數組的下角標(            此公式不固定,是自訂的,但是要保證結果穩定不變,同時不能大於數組size) */        int index = Math.abs(key.hashCode()) % SIZE;        /* 6.當index位置為空白時,填充新的list */        if(buckets[index]==null){            buckets[index] = new LinkedList<MyEntry<K,V>>();        }        /* 7.擷取index位置的list */        LinkedList<MyEntry<K, V>> bucket = buckets[index];        /* 8.MyEntry是自訂的Entry實作類別,用於儲存索引值對,這個類也可以自訂,只要實現介面Entry即可,            建立entry儲存傳入的索引值對 */        MyEntry<K, V> newMe = new MyEntry<K, V>(key,value);        /* 9.定義一個found標記,用於記錄是否替換完畢 */        boolean found = false;        ListIterator<MyEntry<K, V>> it = bucket.listIterator();        /* 10.遍曆當前位置的list */        while(it.hasNext()){            MyEntry<K, V> oldMe = it.next();            /* 11.list中已經存在了當前key */            if(oldMe.getKey().equals(key)){                /* 12.獲得oldValue值,用於返回 */                oldValue = oldMe.getValue();                /* 13.用新的entry替換老的 */                it.set(newMe);                /* 14.標記改為true,說明替換完畢 */                found = true;                /* 15.跳出 */                break;            }        }        /* 16.如果未替換完畢,也就是說key值在之前的list中不存在 */        if(!found){            /* 17.添加新的entry到list中 */            bucket.add(newMe);        }        /* 18.返回oldValue */        return oldValue;    }    /* 19.定義get尋找方法 */    public V get(Object key){        /* 20.產生散列值,也就是數組角標,此處一定要和put方法中產生方式一致,保證相同的key產生相同的位置 */        int index = Math.abs(key.hashCode()) % SIZE;        /* 21.index位置為null,不存在key,返回null */        if(buckets[index]==null){            return null;        }        /* 22.index位置不為null,遍曆查詢,返回value */        for(MyEntry<K,V> me:buckets[index]){            if(me.getKey().equals(key)){                return me.getValue();            }        }        return null;    }    @Override    public Set<Entry<K, V>> entrySet() {        return null;    }}

 

《java編程思想》:散列的原理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.