A priori: 3 interfaces in Java Collections:

  1. List: allow duplicate elements;
  2. Set: not allow duplicates;
  3. Map: ??

Map:

  1. key: not allow duplicates;
  2. value: allow duplicates;

Reason: map is unordered. Finding elements via key. If keys are the same, how to find the desired value?

Truth:

  1. pair: {same key, different values} are allowed in Map.
  2. <K, V>: if key duplicates, which value is taken into Collection?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class DupKV {
public static Map putSome(Map<String, String> map) {
map.put("gender", "male");
map.put("name", "lei");
map.put("name", "kawasaki");
map.put("name", "yamamoto");
map.put("from", "Asia");
return map;
}

public static void main(String[] args) {
System.out.println(putSome(new HashMap<String, String>()));
System.out.println(putSome(new TreeMap<String, String>()));
System.out.println(putSome(new LinkedHashMap<String, String>()));
}
}

Output:

1
2
3
{gender=male, name=yamamoto, from=Asia}
{from=Asia, gender=male, name=yamamoto}
{gender=male, name=yamamoto, from=Asia}

HashMap, TreeMap, LinkedHashMap: whichever subclass it is of Map, the last <K, V> is stored in Collection.