.NET面试题系列(十二)Dictionary原理

序言

 

Dictionary的构造

下面的代码我看看Dictionary在构造时都做了什么:

private void Initialize(int capacity)
        {
            int prime = HashHelpers.GetPrime(capacity);
            this.buckets = new int[prime];
            for (int i = 0; i < this.buckets.Length; i++)
            {
                this.buckets[i] = -1;
            }
            this.entries = new Entry<TKey, TValue>[prime];
            this.freeList = -1;
        }

我们看到,Dictionary在构造的时候做了以下几件事:

  1. 初始化一个this.buckets = new int[prime]

  2. 初始化一个this.entries = new Entry<TKey, TValue>[prime]

  3. Bucket和entries的容量都为大于字典容量的一个最小的质数

其中this.buckets主要用来进行Hash碰撞this.entries用来存储字典的内容,并且标识下一个元素的位置

 

hash冲突怎么办?

 

资料

https://www.cnblogs.com/InCerry/p/10325290.html

https://blog.csdn.net/zhaoguanghui2012/article/details/88105715

posted @ 2018-08-02 16:30  ~沐风  阅读(748)  评论(0编辑  收藏  举报