From the frequency of the use of a simple to say a bit.
Array/arraylist/list/linkedlist
Array
Arrays are first seen in C #. is stored continuously in memory, so its index is very fast, and the assignment and change elements are also very easy.
But there are some deficiencies in the array. Inserting data between the two data in an array is cumbersome, and you must specify the length of the array when declaring the array. The length of the array is too long, resulting in a waste of memory, which can cause data overflow errors. Suppose we don't know the length of an array when declaring an array. It will become very troublesome.
For these drawbacks of arrays, ArrayList objects are first provided in C # to overcome these drawbacks.
The underlying data structure is an array.
ArrayList
ArrayList is part of the namespace System.Collections. References must be made when using the class, and the IList interface is inherited at the same time. Data storage and retrieval are provided. The size of the ArrayList object is dynamically expanded and shrunk according to the data stored in it. So. You do not need to specify the length of a ArrayList object when declaring it.
Look at the sample above. ArrayList seems to have conquered all the shortcomings in the array, why is there a list?
We look at the example above, in the list. Not only did we insert the string CDE. and inserted the number 5678.
This allows the insertion of different types of data in the ArrayList to be agreed.
Since ArrayList will treat all the data in the Insert as an object type, when we use ArrayList to process the data. It is very likely that a type mismatch error is reported, that is, ArrayList is not type-safe. Boxing and unboxing operations typically occur when a value type is stored or retrieved, resulting in very high performance wear and tear.
Concept of packing and unpacking:
To put it simply:
Boxing: is to package data of a value type into an instance of a reference type
For example, the value ABC of string type is assigned to the object obj
Unpacking: Extracting value types from reference data
For example, assigning the value of the object obj to the variable I of type string
The process of packing and unpacking is very lossy.
The underlying data structure is an array. Similar to C + + there is no generic vector.
Generic List
Because ArrayList has the disadvantage of unsafe types and packing unboxing. So there's the concept of generics. The list class is a generic equivalent class of the ArrayList class. Most of its use methods are similar to ArrayList. Because the list class also inherits the IList interface.
The key difference is that when declaring a list collection, we need to declare the object type for the data in the list collection at the same time.
Example:
In the example above. Suppose we insert an int array 123 into the list collection. The IDE will make an error. and cannot be compiled. This avoids the previously mentioned type-safety issues and the performance issues of packing unboxing.
The underlying data structure is an array.
Similar to the vector in C + +.
LinkedList
List implemented with a double-linked list. Feature is insert delete fast, find slow
Linkedlist<t> provides a separate node for the linkedlistnode<t> type, so the insertion and removal of the operation complexity is O (1).
The ability to remove nodes and then insert them again in the same list or in another list. This will not allocate additional objects in the heap.
Because the list also maintains an internal count. So the operation complexity of getting the Count property is O (1).
Each node in a Linkedlist<t> object belongs to the linkedlistnode<t> type. Since Linkedlist<t> is a doubly linked list, each node points forward to the Next node and to the Previous node backwards.
linkedlist<string> list = new linkedlist<string> (); list. AddFirst ("Data Value 1"); list. AddLast ("Data Value 6");
A comparison of the performance of list and lonkedlist
Add Delete
The speed at which nodes are added and removed in list<t>. It is generally faster than the same operation when using Linkedlist<t>. Will List<t>. The Add method is compared to the add* method of linkedlist<t>. The real performance difference is not the add operation, but the pressure on the linkedlist<t> to the GC (garbage collection mechanism). A list<t> is essentially storing its data on an array of stacks. And linkedlist<t> is to keep all its nodes on the stack (others are one, I am a series). This allows the GC to manage a number of other Linkedlist<t> node objects on the stack.
Attention, List<t>. The insert* method may be slower to join a node in any place than using the Add* method in linkedlist<t>. However. This relies on the position of the list<t> insert object. The Insert method must have all the elements behind the insertion point move backward one bit. Assuming that the new element is inserted in the final or near final position of the list<t>, its overhead is negligible relative to the total overhead of the GC maintaining the linkedlist<t> node.
Index
Another place where list<t> performance is better than linkedlist<t> is when you use the index to access the interview.
In list<t>, you can use the index value (indexer) to navigate directly to a detailed element location.
But in the linkedlist<t>, there is no such luxury. In linkedlist<t>, you must traverse the entire list through the previous or next property until you find the node you want.
Hashset/hashtable/dictionary
The bottom of the three containers is the hash table.
HashSet
MSDN is very easy to interpret: represents a set of values.
The Hashset<t> class provides high-performance set operations. A group is a collection that does not include elements of any repetition, and the order of elements is not in sequence.
A hash table was used to store the data. is to Exchange O (n) for time in O (n) space. That is, the time to find the element is O (1).
It includes some set of operations
Hashset<t> provides a number of mathematical setup operations such as Group Join (union), and set subtraction. The following table lists the provided hashset<t> operations and their mathematical equivalents.
Unionwith-union or add its settings to the
Intersectwith-Intersection
Exceptwith-set Subtraction
Symmetricexceptwith-Yu Giu
In addition to the set operations listed, the Hashset<t> class provides methods to determine whether set is equal, overlapping sets, and whether a set is a subset or a superset of a set.
Example
hashset<int> evennumbers = new hashset<int> (); hashset<int> oddnumbers = new hashset<int> (); for (int i = 0, i < 5; i++) { //Populate numbers with just Even numbers. Evennumbers.add (i * 2); Populate oddnumbers with just odd numbers. Oddnumbers.add ((i * 2) + 1);} Create a new HashSet populated with even numbers. hashset<int> numbers = new hashset<int> (evennumbers); Numbers. Unionwith (oddnumbers);
HashTable
Represents a collection of key/value pairs organized by a hash code of the key.
Hashtable is a container provided by the System.Collections namespace that handles and behaves similar key/value key-value pairs, in which key is usually used for high-speed lookups, while key is differentiated between uppercase and lowercase. Value is used to store values corresponding to key. Key/value Key-value pairs in Hashtable are all object types, so hashtable can support whatever type of Key/value key-value pair.
He's got a lot of internal maintenance. For Key-value key-value pairs, there is also a value of a similar index called the hash Value (hashcode), which is obtained by a certain algorithm based on the GetHashCode method. All locate operations are based on hash values to find the corresponding key and value values.
When the occupied space in the current Hashtable reaches a percentage, the space will be expanded on its own initiative. In. Net this percentage is 72%, also called. NET, the fill factor for Hashtable is 0.72.
For example, there is a hashtable space size of 100. This hashtable will be expanded when it needs to add a 73rd value.
What is the size of this self-expanding initiative? The answer is the current space size of twice times the nearest prime, such as the current Hashtable occupied space for a prime number of 71, assuming the expansion, the expansion size is the prime number 131.
Hashtable openwith = new Hashtable ();//ADD Some elements to the hash table. There is no//duplicate keys, but some of the values is DUPLICATES.OPENWITH.ADD ("txt", "notepad.exe"); Openwith.add ("BMP "," Paint.exe "); Openwith.add (" Dib "," Paint.exe "); Openwith.add (" RTF "," Wordpad.exe ");
Hashtable also has the overhead of boxing and unboxing.
And then there's the
Dictionary
Dictionary is also a key-value container. The deposit object is required to be stored in the generic type corresponding to the [key] value one by one. Relative to Hashtable, similar to the relationship between list and ArrayList. It is type-safe.
dictionary<string, string> mydic = new dictionary<string, string> (); Mydic.add ("AAA", "111"); MYDIC.ADD (" BBB "," 222 "); Mydic.add (" CCC "," 333 "); Mydic.add (" ddd "," 444 ");
Summary
The capacity of an array is fixed, you can only get or set the value of an element at a time, and the capacity of ArrayList or list<t> can be expanded, altered, deleted, or inserted on its own initiative.
Arrays can have multiple dimensions, whereas ArrayList or list< t> always have only one dimension. However, you can easily create lists of array lists or lists. The performance of arrays of specific types (except Object) is better than ArrayList. This is because the elements of ArrayList are of type Object, so boxing and unboxing operations typically occur when a value type is stored or retrieved.
The performance of,list< t> is similar to that of an array of the same type, except when no more allocations are needed (that is, the initial capacity is very close to the maximum capacity of the list).
When deciding whether to use list<t> or use the ArrayList class (which has similar functionality), remember that the List<t> class works better and is type safe in most cases.
Assuming that a reference type is used for the type T of the list< t> class, the behavior of the two classes is exactly the same. However, assuming that a value type is used for type T, the implementation and boxing issues need to be considered.
So basically not how to use ArrayList.
One more thing to keep in mind
It's better to use dictionary on a single thread. Multithreading is better when using Hashtable.
Because Hashtable is able to pass Hashtable tab = hashtable.synchronized (New Hashtable ()), a thread-safe object is obtained.
Finally put a SOF above a question about dictionary and Hashtable ...
Why are Dictionary preferred over Hashtable?
FWIW, a Dictionary is a hash table.
If you meant, why does we use the Dictionary class instead of the Hashtable class?
"Then it's an easy answer:dictionary are a generic type, Hashtable is not. That's means you get the type safety with Dictionary, the because you can ' t insert any random object into it, and you don ' t has to Cast the values to take out.
References
Boxing and Unboxing (C # Programming Guide)-https://msdn.microsoft.com/zh-cn/library/yz2be5wk.aspx
The difference between arrays, ArrayList, and List in C #-https://www.google.com.hk/webhp?sourceid=chrome-instant&ion=1&espv=2&ie= utf-8#newwindow=1&safe=strict&q=c%23+hashset+%e5%ba%95%e5%b1%82
Common use of containers in C # and underlying data structures