class declaration:
Overview:
- Thread is not secure;
- <key, value> both can be null;
- The order of the mappings is not guaranteed, especially if it does not guarantee that the order is constant;
- HashMap use iterator;
- The default size of the hash array in HashMap is 16, and the growth pattern must be 2 times the number of points;
HashMap's data structure:
-
Array:
The array storage interval is contiguous, and you can quickly access any element in the array by subscript, again, if you want to delete an element, you need to move a large number of elements, and the insert operation is the same. But the memory is serious, so the space complexity is very large. However, the binary lookup time of the array is less complex, only O (1). The features of the array are: easy addressing, insertion and removal difficulties.
-
Linked list:
The storage interval of the list is discrete, the memory is relatively loose, so the space complexity is very small, but the time complexity is very large, O (N). The list is characterized by difficult addressing, easy insertion and deletion.
-
HashMap's Choice: hash table
Hash tables combine the characteristics of both, making a data structure that is easy to address, and easy to insert and delete. The hash table (hash table) satisfies the convenience of data searching and does not occupy too much content space.
From what we can find is that the hash table consists of an array + linked list, an array of length 16, each of which stores the head node of a linked list. The hash value of the element stored in the array is then obtained by hashing (key)%len, which is the logarithm of the value of the element's key. For example, in a hash table, 12%16=12,28%16=12,108%16=12,140%16=12. So 12, 28, 108, and 140 are all stored in the position labeled 12 below the array.
HashMap implements a static internal class node, which implements the Map.entry interface. The array implemented in HashMap is the contents of Node[],map in node[], as shown in.
Data structure of HashMap in Java