IdentityHashMap 和 HashMap 的区别

xiaoxiao2021-02-27  171

在比较key值的时候是不同的 identityHashMap 在比较key的时候 比较的是地址值 k1==k2 而 hashmap 在比较key的时候是比较的内容 k1 == null ? k2 == null:k1.equals(k2) 都可以存 null值 IdentityHashMap<String, String> ihm = new IdentityHashMap<String, String>();HashMap<String, String> m = new HashMap<String, String>(); /*Integer a = new Integer(123); Integer b = new Integer(123);*/ /*int a = 1; int b = 1;*/ String a = "1"; String b = "1"; ihm.put(a, "123"); ihm.put(b, "234"); m.put(a, "123"); m.put(b, "234"); System.out.println(ihm); System.out.println(m);
转载请注明原文地址: https://www.6miu.com/read-15779.html

最新回复(0)