In-depth analysis of Hashtable, Dictionary, SortedDictionary, SortedList

Source: Internet
Author: User

Let's look at it first.Hashtable.

MSDN: a set of key/value pairs. These key/value pairs are organized according to the hash code of the key.

The Hash algorithm converts any-length input (also called pre- ing and pre-image) into fixed-length output using the Hash algorithm. The output is a Hash value. This type of conversion is a compression ing, that is, the space of hash values is usually much smaller than the input space, and different inputs may be hashed into the same output, instead, it is impossible to uniquely determine the input value from the hash value.
 

A Hashtable object consists of buckets that contain collection elements. A bucket is a virtual sub-group of each element in Hashtable. Compared with searching and searching in most sets, a bucket can make searching and searching easier. Each bucket is associated with a hash code. The hash code is generated using a hash function and is based on the key of this element.

The default filling factor of the Hashtable class is 1.0, but the default filling factor is 0.72. All the load factors input from the constructor are multiplied by 0.72 in the Hashtable class. This is a demanding number. You may increase or decrease the filling factor by 0.01 at some time. Your Hashtable access efficiency may increase or decrease by 50%, because the filling factor determines the size of the hash table, the size of the hash table affects the probability of Key conflicts, which in turn affects the performance. 0.72 is a balanced value obtained by Microsoft through a large number of experiments.

Let's look at some source code of Hashtable:

 

Hashtable. ctor Public Hashtable (): this (0, (float) 1f)
{
}
Public Hashtable (int capacity, float loadFactor)
{
If (capacity <0)
{
Throw new ArgumentOutOfRangeException ("capacity", Environment. GetResourceString ("ArgumentOutOfRange_NeedNonNegNum "));
}
If (loadFactor <0.1f) | (loadFactor> 1f ))
{
Throw new ArgumentOutOfRangeException ("loadFactor", Environment. GetResourceString ("ArgumentOutOfRange_HashtableLoadFactor", new object [] {0.1, 1.0 }));
}
This. loadFactor = 0.72f * loadFactor;
Double num = (float) capacity)/this. loadFactor;
If (num> 2147483647.0)
{
Throw new ArgumentException (Environment. GetResourceString ("Arg_HTCapacityOverflow "));
}
Int num2 = (num & gt; 11.0 )? HashHelpers. GetPrime (int) num): 11;
This. buckets = new bucket [num2];
This. loadsize = (
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.