1. SortedDictionary generic class
The SortedDictionary generic class is a binary search tree with a search operation complexity of O(log n), where n is the number of elements in the dictionary. In this regard, it is withSortedList
Generic classes are similar. These two classes have similar object models and both have the retrieval operation complexity of O(log n). The difference between these two classes is the use of memory and the speed at which elements are inserted and removed:
- SortedList uses less memory than SortedDictionary.
- SortedDictionary performs faster insertion and removal operations on unsorted data: its time complexity is O(log n), while SortedList is O(n).
- If you use sorted data to fill the list at once, SortedList is faster than SortedDictionary.
Each key/value pair can be used asKeyValuePair
Structure is searched, or asDictionaryEntry
By non-genericIDictionary
Search the interface.
As long as the keys are used as keys in SortedDictionary, they must be immutable. Each key in SortedDictionary must be unique. The key cannot be null referenced (Nothing in Visual Basic), but if the value type TValue is a reference type, the value can be null.
SortedDictionary requires a comparator implementation to perform key comparisons. You can use a constructor that accepts the comparer parameter to specifyIComparer
Implementation of generic interface; if no implementation is specified, the default generic comparator is used.. If type TKey is implemented
Generic interface, the default comparator uses this implementation.
The foreach statement in C# language requires the type of each element in the collection. Since each element of SortedDictionary is a key/value pair, the element type is neither the type of the key nor the type of the value. But the KeyValuePair type
2. To use Dictionary collection, you need to import the C# generic namespace
(Assembly: mscorlib)
3. Description of Dictionary
- From a set of keys to a set of values, each added item is composed of a value and its associated keys.
- Any key must be unique
- The key cannot be null referenced null (Nothing in VB). If the value is a reference type, it can be null.
- Key and Value can be of any type (string, int, custom class, etc.)
4. Common usage of Dictionary: Take the type of key as int and the type of value as string as an example
1. Creation and initialization
Dictionary<int,string>myDictionary=newDictionary<int,string>();
2. Add elements
(1,"C#");
3. Find elements through Key
if((1)) { ("Key:{0},Value:{1}","1", myDictionary[1]); }
4. Traverse elements through KeyValuePair
foreach(KeyValuePair<int,string>kvp in myDictionary) ...{ ("Key = {0}, Value = {1}",, ); }
5. Only traverse the keys attributes
Dictionary<int,string>.KeyCollection keyCol=; foreach(intkeyinkeyCol) ...{ ("Key = {0}", key); }
6. Only traverse the value Valus property
Dictionary<int,string>.ValueCollection valueCol=; foreach(stringvalueinvalueCol) ...{ ("Value = {0}", value); }
7. Remove the specified key value through the Remove method
(1); if((1)) ...{ ("Key:{0},Value:{1}","1", myDictionary[1]); } else { ("Not exists Key : 1"); }
5. Description of other common properties and methods:
Comparer: Gets the IEqualityComparer used to determine whether the keys in the dictionary are equal.
Count:
Item: Gets or sets the value associated with the specified key.
Keys: Get a collection containing keys in Dictionary.
Values: Get a collection containing values in Dictionary.
Add: �
Clear: Remove all keys and values from Dictionary.
ContainsKey: Determines whether Dictionary contains the specified key.
ContainsValue: Determines whether Dictionary contains a specific value.
GetEnumerator: Returns the enumeration number that loops through the Dictionary.
GetType: Get the Type of the current instance. (Inherited from Object.)
Remove: Remove the value of the specified key from the Dictionary.
ToString: Returns the String representing the current Object. (Inherited from Object.)
TryGetValue: Gets the value associated with the specified key.
Use for loop to traverse key values
Dictionary<string, int> Dict = new Dictionary<string, int>(); Dict .Add( "a", 1 ); Dict .Add( "b", 2 ); Dict .Add( "c", 3 ); Dict .Add( "d", 4 ); Dict .Add( "e", 5 ); Dict .Add( "f", 6 ); for(int i = 0 ; i < ; i++) { (()[i].Key + ":" + ()[i].Value); }
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links