This article experiences other generic collections except Queue<T> and Stack<T>.
SortedList<TKey, TValue>
SortedList<TKey, TValue> and List<T> are similar. The difference is that the elements of the SortedList collection are sorted. When adding elements to the SortedList collection, you need to add key-value pairs of data. When adding a collection element, first use the "binary search algorithm" to find the appropriate position, and then the element is placed at that position, and all the collection elements behind this position are retreating one by one as a whole.
static void Main(string[] args) { var mySotedList = new SortedList<int, string>(); (1, "Zhang San"); (2,"Li Si"); (3,"Zhao Liu"); (4,"Wang Jiu"); //Judge whether a key is included (1); //Judge whether it contains a certain value ("Zhang San"); //Get the index of a key int keyIndex = (2); //Get the index of a certain value int valIndex = ("Li Si"); //Delete according to the key (3); //Delete according to the index (1); //Find elements based on key string myVal = mySotedList[3]; //Finding elements based on index string myVal1 = [1]; //Find keys based on index int myKey = [3]; //Get an element without throwing an exception string myWantVal; if ((2, out myWantVal)) { } //Travel key foreach (int key in ) { } //Transfer value foreach (string str in ) { } }
The relatively disadvantage of using SortedList<TKey, TValue> is that when inserting data, the insertion position is determined through the "binary search algorithm", which is relatively expensive; however, because the SortedList<TKey, TValue> collection elements are sorted, this makes searching convenient.
If you need to search a collection regularly and insert a relatively small amount of data into the collection, you can consider using SortedList<TKey, TValue>.
Dictionary<TKey,TValue>
Dictionary<TKey,TValue> places the stored data in a HashTable. The key of each collection element must be unique and the collection element is not sorted.
The similarity to SortedList<TKey, TValue> is that it also adds collection elements in the form of key-value pairs, and the difference is that the efficiency of inserting data is higher than that of SortedList<TKey, TValue>.
var myDictionary = new Dictionary<int, string>(); (1, "darren"); (2, "jack"); (3, "sunny"); //Judge whether it exists based on the key (1); //Judge whether it exists based on the value ("darren"); //Delete according to the key (3); //Finding collection elements based on key string myVal = myDictionary[2]; //Follow the element according to the key and do not throw exceptions string myVal1; if ((2, out myVal1)) { } //Travel key foreach (int key in ) { } //Transfer value foreach (string value in ) { }
SortedDictionary<TKey,TValue>
The biggest difference between SortedDictionary<TKey,TValue> and SortedList<TKey,TValue> is that SortedDictionary<TKey,TValue> does not allow collection elements to be obtained through indexes, and it maintains a "red and black tree". Therefore, when performing insertion operations, SortedDictionary<TKey,TValue> has better execution efficiency than SortedList<TKey,TValue>, and of course it also occupies more memory.
var mySortedDictionary = new SortedDictionary<int, string>(); (1,"one"); (2,"two"); (3,"three"); //Judge whether a key is included (2); //Judge whether it contains a certain value ("two"); //Remove an element according to the key (2); //Finding an element based on the key string myVal = mySortedDictionary[1]; //Finding elements according to keys, no exceptions are thrown string myVal1; if ((1, out myVal1)) { } //Transfer all keys foreach (int key in ) { } //Transfer all values foreach (string val in ) { }
LinkedList<T>
Each collection element has attributes pointing to the previous and next nodes. The first node is the last node and the next node is the second node. The previous node of the last node is the second to last node, and the next node is the first node.
var myLinkedList = new LinkedList<string>(); //Create the first node ("one"); //Create the last node var lastNode = ("three"); //Add a node before the last node (lastNode, "two"); //Add a node after the current last node (lastNode, "four"); //Transfer node values foreach (string value in myLinkedList) { } //Finding nodes based on values //Find the first node that meets the criteria var myNode = ("two"); //Finding nodes based on values //Find the last node that meets the criteria var myNode1 = ("one"); //Get the first node var firstNode = ; //Get the last node var lastNode1 = ; //Create nodes according to the value ("one"); //Delete the first node (); //Delete the last node (); //Clear all nodes ();
If you have many insert operations on a collection, or insert batch collection elements, you can consider using LinkedList<T>.
SortedSet<T>
The inside of SortedSet<T> is actually of the SortedDictionary type, but there are no keys, only values. SortedSet represents an abstract dataset, and the element values in the dataset must be unique and unsorted.
If you need to merge a set of data, you can consider using SortedSet<T>.
var sortedSet1 = new SortedSet<string>(); ("one"); ("two"); ("three"); var sortedSet2 = new SortedSet<string>(); ("two"); ("four"); //Judge whether a value exists ("one"); //Merge the data set and remove duplicates (sortedSet2); //Judge whether a data set is the self of another data set (sortedSet2); //Judge whether an i-quota data set contains all elements of another data set (sortedSet2); //Judge whether there is an intersection between the two data sets (sortedSet2); //Delete an element according to the value ("two"); //Judge whether the two data sets are equal (sortedSet2); //Only contained elements in one dataset, these elements are not included in another dataset (sortedSet2); //Take the union of two data sets (sortedSet2); //Get the dataset containing certain elements SortedSet<string> result = ("one", "two"); //Transfer the dataset foreach (string val in sortedSet1) { } //Clear the dataset ();
HashSet<T>
Both HashSet and SortedSet implement ISet interfaces. The prerequisite for using SortedSet is that it needs to be used as a search condition based on multiple element values; the data cannot be hashed. HashSet should be considered for the rest of the situation.
var hashSet1 = new HashSet<string>(); ("one"); ("two"); ("three"); var hashSet2 = new HashSet<string>(); ("two"); ("four"); //Judge whether it contains a certain value ("four"); //Remove items that are repeated in one dataset and another dataset (hashSet2); //Keep the same items in one dataset as another dataset (hashSet2); //Judge whether a data set is a subset of another data set (hashSet2); //Judge whether a data set contains all elements of another data set (hashSet2); //Judge whether there are duplicate elements in the two data sets (hashSet2); //Delete an element according to the value ("one"); //Judge whether the two data sets are equal (hashSet2); //Merge two data sets (hashSet1); //Look for elements that are only included in one dataset but not in another dataset (hashSet2); //Transfer the dataset foreach (string value in hashSet1) { } //Clear the dataset ();
I have experienced the usage of various generic collections these days. The summary is as follows:
Stack<T>In and Out,here。
Queue<T>First in, first out, inhere。
SortedList<TKey, TValue>, inserting data is not very frequent and often needs to be searched.
Dictionary<TKey,TValue>, insert data frequently, does not sort, key-value pairs.
SortedDictionary<TKey,TValue>, often insert data, sort, key-value pairs.
LinkedList<T>, a two-way linked list, insertion is very casual.
SortedSet<T>, a data set, based on multiple element values as a search condition; the data cannot be hashed.
HashSet<T>, dataset, in most cases, uses HashSet to process dataset operations.
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 relevant links below