Java Collection Framework 中的另一分支的顶层接口，日常中最长使用的键值对容器 HashMap 的顶层接口，所以 Map 这个接口是必读的，一起来做阅读理解吧！

### 关于Map

key - value 结构，，类似字典，最长使用的数据结构之一。

### 类注释

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

This interface takes the place of the Dictionary class, which was a totally abstract class rather than an interface.

Map 接口代替了 Dictionary 类，后者是一个抽象类而不是一个接口。

①：The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. ②：The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.

①： Map 提供了三种集合视图：

• 1、 key 的 set 集合
• 2、 values 的集合
• 3、 key -value 键值对映射

②： map 的顺序定义是 map 上迭代器返回元素的顺序。 一些 Map 的实现类 例如 TreeMap返回元素的顺序做出了保证，而 HashMap 则没有。【所以获取 HashMap 的元素顺序每次都可能不一样。】

Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map. A special case of this prohibition（禁令） is that it is not permissible for a map to contain itself as a key . While it is permissible for a map to contain itself as a value, extreme caution is advised: the equals and hashCode methods are no longer well defined on such a map.

①：All general-purpose map implementation classes should provide two "standard" constructors: a void (no arguments) constructor which creates an empty map, and a constructor with a single argument of type Map, which creates a new map with the same key-value mappings as its argument. ②：In effect, the latter constructor allows the user to copy any map, producing an equivalent map of the desired class. There is no way to enforce this recommendation (as interfaces cannot contain constructors) but all of the general-purpose map implementations in the JDK comply.

• 一个无参构造函数
• 一个以 Map 为入参的构造函数，用于创建和传入 Map 具有相同键值对的新 Map

②： 实际上，后者允许用户复制任何传入的 map ，来构造一个与传入 map 相同的 map。

The "destructive（有害的）" methods contained in this interface, that is, the methods that modify the map on which they operate, are specified to throw UnsupportedOperationException if this map does not support the operation. If this is the case, these methods may, but are not required to, throw an UnsupportedOperationException if the invocation would have no effect on the map. For example, invoking the putAll(Map) method on an unmodifiable map may, but is not required to, throw the exception if the map whose mappings are to be "superimposed" is empty.

Some map implementations have restrictions on the keys and values they may contain. For example, some implementations prohibit null keys and values, and some have restrictions on the types of their keys. Attempting to insert an ineligible key or value throws an unchecked exception, typically NullPointerException or ClassCastException. Attempting to query the presence of an ineligible key or value may throw an exception, or it may simply return false; some implementations will exhibit the former behavior and some will exhibit the latter. More generally, attempting an operation on an ineligible key or value whose completion would not result in the insertion of an ineligible element into the map may throw an exception or it may succeed, at the option of the implementation. Such exceptions are marked as "optional" in the specification for this interface.

• 某些 Map 实现对 key 和 value 的值进行了限制，例如禁止 null keynull value
• 某些实现对 key 的类型有限制

①：Many methods in Collections Framework interfaces are defined in terms（依据） of the equals method.②： For example, the specification for the containsKey(Object key) method says: "returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k))." ③：This specification should not be construed（理解为） to imply（暗示） that invoking Map.containsKey with a non-null argument key will cause key.equals(k) to be invoked for any key k. Implementations are free to implement optimizations whereby the equals invocation is avoided, for example, by first comparing the hash codes of the two keys. (The Object.hashCode() specification guarantees that two objects with unequal hash codes cannot be equal.) ④：More generally, implementations of the various（各种各样的） Collections Framework interfaces are free to take advantage of the specified behavior of underlying（相关） Object methods wherever the implementor deems（认为） it appropriate（合适地）.

① ： Collection Freamework 中的许多方法都是根据 equals 方法来定义的。【毕竟判断两个对象是否相等是相当核心的特性】

② ： 例如， Map 中的 containsKey(Object key) 方法规范这样定义：当且仅当 map 中保存的映射有 k 等于传入的 key 时，返回 true。 就想这样判断： (key == null ? k == null : key.equals(k))

③ ： 这个规范 不应该理解为 暗示使用非空参数调用 Map.containsKey 会导致对 map 中的所有键k 调用 key.equals(k)。 Map 的具体实现类可以自由地进行优化，从而避免 equals 方法被调用，例如 首先比较两个键的 哈希吗，（Object.hasCode() 规范保证了两个对象如果哈希吗不同，则一定不相等）

④ ： 一般来说，各种 Collection Framework 的具体实现类可以在实现者认为合适的地方随意利用底层 Object 的方法指定行为。

Some map operations which perform recursive（循环、递归） traversal（遍历） of the map may fail with an exception for self-referential instances（自引用实例） where the map directly or indirectly contains itself. This includes the clone(), equals(), hashCode() and toString() methods. Implementations may optionally handle the self-referential scenario, however most current implementations do not do so.
This interface is a member of the Java Collections Framework.

• clone
• equals
• hashCode
• toString

#### 要点小结：

1、 Map 中不能包含重复的 key。

2、 Map 提供了三种集合视图：

• 1、 keyset 集合
• 2、 values 的集合
• 3、 key -value 键值对映射

3、 Map 不允许将自身作为 key，但是可以作为 value。

4、 Map 的构造函数规范：

• 一个无参构造函数
• 一个以 Map 为入参的构造函数，用于创建和传入 Map 具有相同键值对的新 Map

5、调用 Map 不支持的方法会抛出 UnsupportedOperationException 异常

6、 某些 Map 会对 key 和 value 的值进行限制，例如禁止 null key / null value，或者只能添加某种类型的 key / value

7、尝试添加非法参数会导致 空指针异常或类型转换异常，尝试查询非法参数根据 Map 的实现可能抛出异常也可能返回 false

8、Map 中的很多方法都依赖 Object 中的 equals

9、Map 中包含自身的自依赖情况需要对：

• clone
• equals
• hashCode
• toString

Q.E.D.