在Map的几种实现里,HashMap、LinkedHashMap 等存放键值对时,key是不允许重复的。如果已存在该key,后put进来的键值对会覆盖已存在的键值对。
public static void main(String[] args) {
Map<String, String> identityHashMap = new IdentityHashMap<String, String>();
identityHashMap.put(new String("1"), "a");
identityHashMap.put(new String("2"), "b");
identityHashMap.put(new String("1"), "c");
Iterator iterator = identityHashMap.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry entry = (Map.Entry)iterator.next();
System.out.println("key: "+entry.getKey()+" vlaue: "+entry.getValue());
}
}