天天看點

使用可變對象作為Java Map的key,會帶來潛在風險的一個例子

package hashMap;

import java.util.HashMap;
import java.util.Map;

/* 
 * 可變對象是指建立後自身狀态能改變的對象。換句話說,可變對象是該對象在建立後它的哈希值可能被改變。

在下面的代碼中,對象MutableKey的鍵在建立時變量 i=10 j=20,哈希值是1291。

然後我們改變執行個體的變量值,該對象的鍵 i 和 j 從10和20分别改變成30和40。現在Key的哈希值已經變成1931。

顯然,這個對象的鍵在建立後發生了改變。是以類MutableKey是可變的。
 */
class MutableKey {
    private int i;
    private int j;

    public MutableKey(int i, int j) {
        this.i = i;
        this.j = j;
    }

    public final int getI() {
        return i;
    }

    public final void setI(int i) {
        this.i = i;
    }

    public final int getJ() {
        return j;
    }

    public final void setJ(int j) {
        this.j = j;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + i;
        result = prime * result + j;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj == null) {
            return false;
        }
        if (!(obj instanceof MutableKey)) {
            return false;
        }
        MutableKey other = (MutableKey) obj;
        if (i != other.i) {
            return false;
        }
        if (j != other.j) {
            return false;
        }
        return true;
    }
}


public class objectAsKey {

    private static void simulateObjectLost() {
        // HashMap
        Map<MutableKey, String> map = new HashMap<>();
        
        // Object created
        MutableKey key = new MutableKey(10, 20);
        
        // Insert entry.
        map.put(key, "Robin");
        
        // This line will print 'Robin'
        System.out.println(map.get(key));
        
        // Object State is changed after object creation.
        // i.e. Object hash code will be changed.
        key.setI(30);
        
        // This line will print null as Map would be unable to retrieve the entry.
        System.out.println(map.get(key));
    }
    
    public static void main(String[] args) {
        // Object created
        MutableKey key = new MutableKey(10, 20);
        System.out.println("Hash code: " + key.hashCode());
        // Object State is changed after object creation.
        key.setI(30);
        key.setJ(40);
        System.out.println("Hash code: " + key.hashCode());
        
        simulateObjectLost();
    }
}