HashMap understanding (from Map)

HashMap understanding (from Map )

Map

1. There is no duplicate key (on the one hand, the key is saved with a set, so the key must be unique and unordered; on the other hand, the value of the map is basically to obtain the value through the key, if there are two identical keys, The computer will not know which corresponding value to obtain; at this time, it may be asked, why can I use the put () method to pass in two key-value pairs with the same key value when programming? That is because the key value is passed in the source code The same key-value pair will be treated as an overlay)

2. Each key can only correspond to one value, multiple keys can correspond to one value (this is the concept of mapping, the most classic example is archery, a row of archers, a row of arrows target, a shooter can only hit one arrow target , And each arrow target may be shot by a different shooter. Here each shooter has only one arrow, there is no such operation of three arrows returning all the targets at the same time. Connect the shooter to the target, this line Adding a shooter and a target is a mapping)

3. Key and value can be data of any reference type (including null) (only reference type)

4. Map replaces the ancient Dictionary abstract class (just know it, you can ignore it)

HashMap

  1. The underlying implementation is a linked list array. After JDK 8, red and black trees are added to implement all Map methods. The key is stored with Set, so I want to do key
    2. Duplication is not allowed. The class corresponding to key (usually String) needs to rewrite hashCode And equals method
  2. Allow empty keys and empty values ​​(but there is only one empty key, and it is placed in the first place, just know it)
  3. List item elements are unordered, and the order will be changed from time to time (after each expansion, it will be re-hashed, that is, the key will be calculated by the hash function to get a different hash value than before, which results in hashing The elements in the table have no order and will change at any time. This is because the hash function is related to the capacity of the bucket array. Each time the node reaches the critical value, it will be automatically expanded. After the expansion, the capacity of the bucket array will be multiplied by two, and the key If it does not change, then the hash value must change.
    4 The time complexity of insertion and retrieval is basically O (1) (provided that there is an appropriate hash function to allow the elements to be distributed in a uniform position)
    The time required to traverse the entire Map 5 It is proportional to the length of the array (hence the capacity of HashMap during initialization should not be too large) Two key factors: initial capacity, loading factor HashMap is not synchronized, HashTable is synchronized, but HashTable has been deprecated, if you need thread safety, you can use synchronizedMap , Such as Map m = Collections.synchronizedMap (new HashMap (…)); method

Quote

Published 1 original article · liked 0 · visits 15

Guess you like

Origin blog.csdn.net/code_mzh/article/details/105394327