when add key-value pair to a hashMap, why Java makes hashMap's hashCode change?
问题 If you look at java's hashCode method inside hashMap, you will find: public int hashCode() { int h = 0; Iterator<Entry<K,V>> i = entrySet().iterator(); while (i.hasNext()) h += i.next().hashCode(); return h; } So when you insert things into hashMap, hashMap's hashCode will change. Thus, if we insert an empty hashMap into hashSet, then insert something to this hashMap, then call hashSet.contains(hashMap) , it will return false . Why does Java allow such behavior? This will easily cause