Do C# dictionaries use hashing in the same manner as Java HashMaps?

问题内容:

I understand that Java HashMap elements are stored in “buckets” based on the hashes of the elements’ keys. Does that same hashing occur in C# dictionaries? If not, then how does lookup work?

问题评论:

    
    
@maytham-ɯɐɥʇʎɐɯ aren’t Dictionaries different from Hashtables, though?
– Justin Borromeo
19 hours ago
1  
Yes, C# Dictionary<K, V> works the same way. Buckets are used to split the space of hashes into smaller, more manageable chunks, and deal with hash collisions. Number of buckets is the first prime number larger than the dictionary capacity (so, when you insert the first item into the dictionary it will contain 3 buckets), and the items are rearranged into new buckets whenever there is a need to increase the capacity.
    
Why are you asking, just in case this is a XY Problem – meta.stackexchange.com/questions/66377/what-is-the-xy-proble‌​m ?
2  
The source code is available; if you want to know exactly how it works, read the sources.

答案:

答案1:

There are several kind of Dictionarys inside C# System.Collections – namespace. They use different strategies to store theire internal data:

This one System.Collections.Specialized.HybridDictionary
uses DoubleLinkedLists until a certain size is reached, then switches to Hashsets. The “normal” System.Collections.Generics-Dictionary uses Hashsets internally all the time. There is also a Dictionary for concurrent uses – look it up yourself (Concurrent Dictionary)if you like.

So it depends on what kind of Dictionary you are using and (in above case) might change due to internal considerations of the class you are using for performance or other reasons.

答案评论:

原文地址:

https://stackoverflow.com/questions/47742954/do-c-sharp-dictionaries-use-hashing-in-the-same-manner-as-java-hashmaps

添加评论