读完 Map 接口之后,就可以去读 Map 的抽象类了,抽象类一般实现了接口最通用的功能,这样具体的实现类就不用再重复去实现了。
类注释
This class provides a skeletal implementation of the Map interface, to minimize the effort required to implement this interface.
AbstractMap
是 Map
接口的一个基本实现,最大程度减少 Map 接口实现类需要做的工作。
①:To implement an unmodifiable map
, the programmer needs only to extend this class and provide an implementation for the entrySet
method, which returns a set-view
of the map's mappings
.②: Typically, the returned set
will, in turn(依次、轮流), be implemented atop AbstractSet
. This set should not support
the add
or remove
methods, and its iterator
should not support the remove method
.
①:
如果要实现一个不可变的 Map
类,开发者只需要继承 AbstractMap
,并且提供 entrySet
方法的实现,该方法返回 Map
的集合视图【public abstract Set<Entry<K,V>> entrySet()
;】。
②
: 通常,返回的集合将依次在 AbstractSet
上实现,这个实现不支持 add
或 remove
方法,并且其迭代器不支持 remove
方法。【也就是集合本身不支持添加/删除元素,迭代器不支持删除元素】
To implement a modifiable map
, the programmer must additionally
override this class's put
method (which otherwise throws an UnsupportedOperationException
), and the iterator
returned by entrySet()
. iterator()
must additionally
implement its remove
method.
如果要实现一个可变的 Map,开发者必须额外覆盖 put
方法,否则会抛出 UnsupportedOperationException
异常。并且 迭代器由 entrySet()
方法返回,迭代器也需要额外实现 remove
方法。
The programmer should generally provide a void (no argument)
and map constructor
, as per the recommendation in the Map interface specification
.
The documentation
for each non-abstract method
in this class describes its implementation in detail. Each of these methods may be overridden if the map being implemented admits a more efficient implementation.
开发者通常根据 Map 规范提供两种构造函数:
- 无参构造函数
- 入参是一个 Map 类型的构造函数
AbstractMap
中的每个非抽象方法的文档都详细描述了方法的实现,如果子类需要更有效的方法实现,则可以覆盖每个抽象类中提供的方法。
类结构
可以看到, AbstractMap
分为几大结构:
- 内部类
SimpleEntry
- 内部类
SimpleImmutableEntry
---> 不可变 - 继承 接口 Map 的方法
- Object 中的方法
- 保存 keySet 和 values 的字段
Q.E.D.
Comments | 0 条评论