What is a hash table?
A hash table (hash table, also known as a hash list) is a data structure that is accessed directly from key. That is, it accesses the record by mapping the key to a location in the table to speed up the lookup. This mapping function is called a hash function, and the array that holds the record is called the hash table. Why do you want to know the dictionary class? 1.HashMap Performance Best Find, add, delete the time complexity is constant.
Data Structure |
Add |
Find |
Delete |
Getbyindex
|
| Array (t[]) |
O (N) |
O (N) |
O (N) |
O (1) |
| Linked list (linkedlist<t>) |
O (1) |
O (N) |
O (N) |
O (N) |
| Resizable array list (list<t>) |
O (1) |
O (N) |
O (N) |
O (1) |
| Stack (stack<t>) |
O (1) |
- |
O (1) |
- |
| Queue (queue<t>) |
O (1) |
- |
O (1) |
- |
| Hash table (dictionary<k,t>) |
O (1) |
O (1) |
O (1) |
- |
tree-based Dictionary (sorteddictionary<k,t>) |
O (log n) |
O (log n) |
O (log n) |
- |
Hash table based set (hashset<t>) |
O (1) |
O (1) |
O (1) |
- |
Tree based set (sortedset<t>) |
O (log n) |
O (log n) |
O (log n) |
- |
2. Extrapolate. HashSet is based on HashMap, the operation is very simple, more like the HashMap did a "package", and only use the HashMap key to achieve a variety of features () HashMap principle?
The hash table is actually very simple, that is, the key through a fixed algorithm function is called a hash function into an integer number, and then the number of the logarithm of the array length of the remainder, the remainder of the result as the subscript, the value is stored in the number as subscript in the array space.
When querying using a hash table, the hash function is used again to convert the key to the corresponding array subscript, and to locate the space to get value, so that we can make full use of the positioning performance of the array for data positioning
. Put method:
Public V put (K key, V value) {//Handle key is Null,hashmap allow key and value to nullif (key = = null) return Putfornullkey (value);//get Hash of key code int hash = hash (key),//hash code to calculate the bucketindexint i = indexfor (hash, table.length);//Remove elements from the Bucketindex position, and loop a single linked list, Determine if key already exists for (entry<k,v> e = table[i]; e = null; e = e.next) {Object k;//hash code is the same and the object is the same if (E.hash = = Hash && ; ((k = e.key) = = Key | | key.equals (k))) {//new value replaces old value and returns old value v OldValue = E.value;e.value = Value;e.recordaccess (this); return oldValue;}} When key does not exist, add new element modcount++;addentry (hash, key, value, i); return null;}
The characteristics of the array are: easy addressing, insertion and deletion difficulties, and the list is characterized by: difficult to address, insert and delete easy. Can we combine the characteristics of both to make a data structure that is easy to address, insert and delete? The answer is yes, this is the hash table we are going to mention, the hash table has a number of different implementations, and I will explain the most commonly used method-the Zipper method, which we can understand as "an array of linked lists." Comparison with other collections: 1. Features comparison:
|
Ordered No |
Allow elements to repeat no |
Collection |
Whether |
Is |
List |
Is |
Is |
Set |
Abstractset |
Whether |
Whether |
HashSet |
TreeSet |
Yes (sort by binary tree) |
Map |
Abstractmap |
Whether |
Using Key-value to map and store data, key must be unique and value can be duplicated |
HashMap |
TreeMap |
Yes (sort by binary tree) |
2.HashMap and Hashtable difference: HashMap is non-synchronized, and Hashtable is synchronized (for the entire table lock, poor performance), HashMap can accept null ( HashMap can accept null key values (key) and values (value), while Hashtable does not). 3. Because HashMap is not thread-safe, use CONCURRENTHASHMAP in a multithreaded environment. Reason: The key is to use the lock separation technology, Concurrenthashmap use lock bucket, the default hash table is divided into 16 barrels, thread operation lock a bucket, so that up to 16 threads simultaneously write. greatly improve concurrency. In terms of reading, Concurrenthashmap uses weakly consistent iterators to innovate new data when the data changes when the iterator is created, and then replace the head pointer with the new data when it is completed. This guarantees the continuity and extensibility of the write. Action: Traverse: Map map = new HashMap ();
Iterator iter = Map.entryset (). Iterator ();
while (Iter.hasnext ()) {
Map.entry Entry = (map.entry) iter.next ();
Object key = Entry.getkey ();
Object val = Entry.getvalue (); Delete: We can find that dictionary is adding, deleting elements as follows: hash algorithm to calculate to the specified bucket, Collide all the data on the same bucket to form a single linked list by default entries data in the slot is added in the order that the deleted data will form a LinkedList linked list, adding data to the LinkedList linked list when the data is added, LinkedList is empty and is sorted by count. HashMap stores key-value pairs of objects in each LinkedList node. Reference: Http://developer.51cto.com/art/201507/485988.htm http://kb.cnblogs.com/page/189480/http://www.cnblogs.com/ gaochundong/p/3813252.html http://referencesource.microsoft.com/#mscorlib/system/collections/generic/ Dictionary.cs,1020c75d4d9aae74 http://visualgo.net/http://www.admin10000.com/document/3322.html/HTTP blog.csdn.net/liuzhengkang/article/details/2916620
Implementation of Dictionary (HASHMAP)