java HashMap insert duplicate Key value problem

To insert duplicate values ​​in HashMap, you first need to figure out how elements are stored in HashMap.
The put method
Each element stored in the Map is a key-value pair, which is added by the put method, and the same key will only have one value associated with it in the Map. The definition of put method in Map is as follows.

V put(K key, V value);

The put() method is implemented: first, hash(key) gets the hashcode() of the key, and the hashmap finds the chain where the position to be inserted is located according to the obtained hashcode. In this chain, all Entry key-value pairs with the same hashcode are placed. After this chain, the equals() method is used to determine whether there is a key-value pair to be inserted, and this equals comparison is actually the key.

It is used to store a key-value pair such as key-value. The return value is the old value stored by the key in the Map, or null if it does not exist before. The put method of HashMap is implemented like this.

public V put(K key, V value) {
    if (key == null)
        return putForNullKey(value);
    int hash = hash(key);
    for (Entry<K,V> e = table[i]; e != null; e = e.next) {
        Object k;
        if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
            V oldValue = e.value;
            e.value = value;
            e.recordAccess(this);
            return oldValue;
        }
    }

    modCount++;
    addEntry(hash, key, value, i);
    return null;
}

From the above, we can see that when adding the corresponding key-value combination, if the corresponding key already exists, the corresponding value is directly changed and the old value is returned, and when judging whether the key exists, it is first compared. The hashCode of the key, and then compare equal or equals.
Directly from the above code, it is a comparison of the hashCode of Map.Entry and the hashCode of the key. In fact, the hashCode of Map.Entry is actually the hashCode of the key. If the corresponding key does not exist originally, addEntry will be called to add the corresponding key-value to the Map. The parameter hash passed by addEntry is the hashCode of the corresponding key.
Implementing adding duplicate elements
Through the study of the put() method, we can find that when judging whether the key exists, the hashCode of the key is compared first, and then the equality or equals is compared, so the hashCode() and equals() methods can be rewritten. Implements adding repeating elements.

@Override
public int hashCode(){                 
     return this.arga.hashCode() * this.argb.hashCode() ; 
} 

@Override
public boolean equals(Object obj) {   
    if (this == obj) {               
        return true;                  
    }         
    if (!(obj instanceof MyType)) {  
        return false;               
    }    
    MyType p = (MyType) obj;  
    if (this.arga.equals(p.arga) && this.argb.equals(p.argb)) {              
        return true ;                  
    } else {           
        return false ;                
    }       
}

After rewriting these two methods, the duplicate key-value pairs can be overwritten. If the value needs to be superimposed, use the containsKey() method to determine whether there are duplicate key values ​​before calling the put() method, and if so, use get() The method gets the original value, plus the newly added value.

The article is referenced from: https://my.oschina.net/elim1/blog/811867
http://blog.csdn.net/dajian790626/article/details/13628137

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325279094&siteId=291194637