Explain how to choose to use ArrayList HashTable List Dictionary arrays

  • 2021-11-13 17:56:41
  • OfStack

In C #, arrays often don't meet our development needs because they are fixed-length.

Because this restriction is inconvenient, ArrayList appeared.

ArrayList, List < T >

ArrayList is a variable length array. You can add any number of data Add to ArrayList. When the length of the array maintained internally is insufficient, it will automatically expand to double the original size.

However, ArrayList also has a disadvantage, that is, the data stored in ArrayList is of Object type, so if the value type is stored and taken out, packing and unpacking operations (that is, the conversion between the value type and the reference type) will occur, which will affect the program performance. After the. Net 2.0 generic type appeared, List was provided < T > .

List < T > It is a generic version of ArrayList. It no longer needs to be packed and unpacked, taken directly and used directly. It is basically the same as ArrayList1. However, when using it, it is necessary to set its type first. After setting the type, it is not this type of data, and Add is not allowed to enter.

As far as performance is concerned, if there is only one kind of data to be stored in the array, then there is no doubt that List < T > Is the best choice.

List < int > ListInt = new List < int > ();

If you have a variable length array, you need to store int and string. Then you can only use ArrayList.

HashTable (Hash Table), Dictionary < T,T >

HashTable is a key-value data structure which can find very quickly according to key, and can't repeat key. Because of its characteristics, its length is always 1 prime number, so the capacity after expansion will be 1 point larger than 2 times, and the loading factor is 0.72 f.

When a large number of key are used to find value, HashTable is undoubtedly the most selective. HashTable and ArrayList1 are non-generic. value is stored as object, and packing and unpacking will occur when accessing, so Dictionary appears < T,T > .

Dictionary < T,T > It is a generic version of HashTable, which is equally fast to access, but does not need to be boxed and unpacked. Moreover, it optimizes the algorithm, Hashtable is 0.72, and its wasted capacity is much less.

Dictionary<string,Person> Dic = new Dictionary<string,Person>();

HashSet < T >

HashSet < T > Class, algorithm and storage structure are the same as hash table, which is mainly designed to do high-performance set operation, such as intersection, union and difference set of two sets. The collection contains 1 set of elements that do not recur and have no specific order.

Queue, Queue < T >

Queue queue, Queue < T > Generic queues, which have been learned in universities, queues, first in, first out, are very useful.

Stack, Stack < T >

Stack stack, first in, then out.

SortedList, SortedList < TKey,TValue >

The data in the SortedList collection is ordered. Data can be matched by key or obtained by int subscript.

The addition operation is slightly slower than ArrayList and Hashtable. Find and delete operations are faster than ArrayList and slower than Hashtable.

SortedDictionary < TKey,TValue >

SortedDictionary < TKey,TValue > Compared with SortedList < TKey,TValue > Its performance is optimized, SortedList < TKey,TValue > It maintains arrays internally and SortedDictionary < TKey,TValue > Internal maintenance is a red-black tree (balanced 2-tree), so its memory, performance is better than SortedDictionary < TKey,TValue > . Only 1 difference cannot be taken by subscript.

ListDictionary (Unidirectional Linked List), LinkedList < T > (Double linked list)

List < T > , ArrayList, Hashtable and other container classes, which internally maintain the arrays Array, ListDictionary, and LinkedList < T > Instead of Array, it is saved in the form of linked list. The biggest advantage of linked list is to save memory space.

ListDictionary is a unidirectional linked list.

LinkedList < T > Double linked list. Advantages of double linked list, can be inserted into any position.

HybridDictionary

The class of HybridDictionary makes full use of the advantages of high query efficiency of Hashtable and less memory space of ListDictionary, and has built-in Hashtable and ListDictionary containers. The internal logic when adding data is as follows:

When the amount of data is less than 8, Hashtable is null, and ListDictionary is used to save data.

When the amount of data is greater than 8, Hashtable is instantiated, the data is transferred to Hashtable, and then ListDictionary is set to null.

BitArray

BitArray is used for binary operations, such as "OR", "NOT", "AND", "XOR NOT", etc. Only true or false can be stored;

Application scenario

ArrayList,List < T > Variable-length arrays;

HashTable,Dictionary < T,T > Find value frequently according to key;

HashSet < T > Set operation;

Queue, Queue < T > First in, first out;

Stack, Stack < T > Stack, FIFO;

SortedList, SortedList < TKey,TValue > Hash table, to pass the subscript, but also through key value, can be selected;

ListDictionary: Unidirectional linked list, which should be traversed every time data is added. When the amount of data is large, the efficiency is low, and when the amount of data is large and frequently inserted, it should not be selected.

LinkedList < T > Two-way linked list;

HybridDictionary: Available when the amount of data is unknown.

SortedDictionary < TKey,TValue > : SortedList < TKey,TValue > The optimized version of, the internal array turns into a balanced 2-tree.

BitArray: Optional for binary operation;

The above is the whole content of this article, hoping to help everyone!


Related articles: