A priori: 3 interfaces in Java Collections:
- List: allow duplicate elements;
- Set: not allow duplicates;
- Map: ??
Map:
- key: not allow duplicates;
- value: allow duplicates;
Reason: map is unordered. Finding elements via key. If keys are the same, how to find the desired value?
Truth:
- pair: {same key, different values} are allowed in Map.
- <K, V>: if key duplicates, which value is taken into Collection?
1 | public class DupKV { |
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.