java:Map借口及其子类HashMap五,identityHashMap子类
了解:identityHashMap子类
一般情况下,标准的Map,是不会有重复的key值得value的,相同的key是被覆盖的。HashMap
IdentityHashMap是个例外,他允许Key值重复,只有当两个对象用“==”来判断时,才能判断他相等。
Map<String, Integer> map = new IdentityHashMap<String,Integer>();
map.put(new String("zhangsan"), 30);
map.put(new String("zhangsan"), 31);
System.out.println(map);
结果:{zhangsan=30, zhangsan=31}
Iterator输出:
Map<String, Integer> map = new IdentityHashMap<String,Integer>();
map.put(new String("zhangsan"), 30);
map.put(new String("zhangsan"), 31); Set<String> keys = map.keySet();
Iterator<String> iter = keys.iterator();
while(iter.hasNext())
{
String key = iter.next();
System.out.println( key + "-->" + map.get(key) );
} System.out.println(map);
结果:
zhangsan-->30
zhangsan-->31
{zhangsan=30, zhangsan=31}