MAP is mainly used to store the key-value pairs and obtain values based on the keys. Therefore, duplicate keys are not allowed (repeated overwrites), but repeated values are allowed.
Hashmap is the most commonly used map. It stores data based on the hashcode value of the key, and can directly obtain its value based on the key, with fast access speed and time, the order in which data is obtained is completely random. Hashmap allows a maximum of null keys for one record, and null values for multiple records. hashmap does not support thread synchronization, that is, multiple threads can write hashmap simultaneously at any time; data inconsistency may occur. If synchronization is required, you can use the synchronizedmap method of collections to synchronize hashmap or use concurrenthashmap.
Like hashmap, hashtable inherits from the dictionary class. The difference is that it does not allow null keys or values to be recorded. It supports thread synchronization, at any one time point, only one thread can write hashtable, which also causes the write speed of hashtable to be slow.
Linkedhashmap stores the record insertion sequence. When iterator is used to traverse linkedhashmap, the first record must be inserted first. You can also use parameters to sort the records by the number of times of application during construction. It is slower than hashmap during traversal, but in some cases, when the hashmap capacity is large and the actual data is small, the traversal may be slower than linkedhashmap, because the traversal speed of linkedhashmap is only related to the actual data and has nothing to do with the capacity, and the traversal speed of hashmap is related to its capacity.
Treemap implements the sortmap interface, which can sort the records stored by the key. By default, It is the sort of key values in ascending order. You can also specify the sort comparator. When you use iterator to traverse treemap, the obtained records are sorted in ascending order.
Generally, hashmap is used most often. The key-value pairs stored in hashmap are random when they are retrieved. It stores data based on the key's hashcode value, you can directly obtain its value based on the key, with fast access speed. Hashmap is the best choice for inserting, deleting, and locating elements in a map.
Treemap obtains the sorted key-value pairs. However, if you wantTraverse keys in natural or custom order, Then treemap will be better.
Linkedhashmap is a subclass of hashmap.The output sequence is the same as that of the input.Then, you can use linkedhashmap to implement it. It can also be arranged in the read order, as it can be applied in the connection pool.
1. hashset is implemented through hashmap, and treeset is implemented through treemap, except that set only uses the map key.
2. The key and set of map have a common feature that is the uniqueness of the set. treemap has an additional sorting function.
3. hashcode and equal () are used by hashmap. Because no sorting is required, you only need to pay attention to location and uniqueness.
A. hashcode is used to calculate the hash value, and the hash value is used to determine the index of the hash table.
B. An index in the hash table stores a linked list. Therefore, you must use the equal method to cyclically compare each object in the chain.
To locate the entry corresponding to the key value.
C. Put: If the hash table is not located, add an entry in front of the linked list. If it is located, replace the value in the entry and return the old value.
4. Because treemap needs to be sorted, a comparator is required to compare the size of the key value. Of course, it is also located using comparator.
A. comparator can specify
B. If it is not determined during creation, the key. compareto () method will be used, which requires that the key must implement the comparable interface.
C. treemap is implemented using the tree data structure, so you can use the Compare interface to complete the positioning.
Note:
1. collection does not have the get () method to obtain an element. You can only use iterator () to traverse elements.
2. Set and collection have the same interface.
3. list. You can use the get () method to retrieve an element at a time. Use numbers to select one of a bunch of objects, get (0 ).... (Add/get)
4. arraylist is generally used. Use the queue list to construct the stack and queue.
5. Map uses put (K, v)/get (K) and containskey ()/containsvalue () to check whether a key/value exists.
Hashmap uses the hashcode of the object to quickly find the key.
* Hashing
The hash code transforms the object information to form a unique int value, which is stored in an array.
We all know that the array search speed is the fastest in all storage structures. Therefore, the search can be accelerated.
When a collision occurs, let the array point to multiple values. That is, an orders table is generated at each position of the array.
6. Elements in map can be extracted separately from the key sequence and value sequence.
Use keyset () to extract the key sequence and generate a set for all the keys in the map.
Use values () to extract the value sequence and generate a collection for all values in the map.
Why does one generate a set and one generate a collection? That's because the key is always unique and the value can be repeated.
PS: http://seaizon.iteye.com/blog/571101