java Map with Integer value comparison [duplicate]

Solution 1:

This is because it's caching by JVM. In other words for all values [-128; 128). Integer.valueOf(i) will return the same Integer instance.

public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}