在正常的map操作中,key是不能重复的,如果希望key的内容可以重复,可以用IdentityHashMap
举个栗子
输出结果:
public static void main(String[] args) { Map<String,String> map = new HashMap<>(); map.put("姓名","小明"); map.put("姓名","小红"); map.put("姓名","张三"); map.put("姓名","李四"); System.out.println("普通map:"+map); Map<String, String> IdentityHashMap = new IdentityHashMap<>(); IdentityHashMap.put(new String("name"),"路飞"); IdentityHashMap.put(new String("name"),"小叮当"); IdentityHashMap.put(new String("name"),"圣斗士"); System.out.println("key可重复map:"+IdentityHashMap); }