散列算法已更改.这意味着您不能依赖迭代顺序java.util.HashMap
.这不应该让人感到意外,JDK从来没有给出任何这样的保证.如果订单对您很重要,请使用TreeMap
或LinkedHashMap
.
JDK5 HashMap:
static final int hash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); }
JDK7 HashMap:
final int hash(Object k) { int h = hashSeed; if (0 != h && k instanceof String) { return sun.misc.Hashing.stringHash32((String) k); } h ^= k.hashCode(); // This function ensures that hashCodes that differ only by // constant multiples at each bit position have a bounded // number of collisions (approximately 8 at default load factor). h ^= (h >>> 20) ^ (h >>> 12); return h ^ (h >>> 7) ^ (h >>> 4); }