Map通过的方式来存储数据,key不允许相同。 HashMap通过equal来比较key的值是否相同,而IdentityHashMap通过==来比较key的引用地址是否相同。
所以:
Map hashMap=new HashMap();
hashMap.put(new String("张三"), "100");
hashMap.put(new String("张三"), "100");
System.out.println(hashMap.size());
打印的结果为1。
而
Map identityHashMap=new IdentityHashMap();
identityHashMap.put(new String("张三"), "100");
identityHashMap.put(new String("张三"), "100");
System.out.println(identityHashMap.size());
打印的结果为2。
WeakHashMap同HashMap ,只是WeakHashMap中的条目在无调用的情况下会自动销毁。
所以:
Map hashMap=new HashMap();
hashMap.put(new String("张三"), "100");
hashMap.put(new String("张三"), "100");
System.out.println(hashMap.size());
打印的结果为1。
而
Map identityHashMap=new IdentityHashMap();
identityHashMap.put(new String("张三"), "100");
identityHashMap.put(new String("张三"), "100");
System.out.println(identityHashMap.size());
打印的结果为2。
WeakHashMap同HashMap ,只是WeakHashMap中的条目在无调用的情况下会自动销毁。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28624388/viewspace-763774/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/28624388/viewspace-763774/