Java集合框架07-Map整体架构

1、Map的整体结构

Map是一种把键对象和值对象映射的集合,它的每一个元素都包含一对键对象和值对象。 Map没有继承Collection接口。

  • AbstractMap:实现了Map接口的抽象类。Map的基本实现,其他Map的实现类可以通过继承AbstractMap来减少编码量。
  • SortedMap:继承Map。保证按照键的升序排列的映射,对entrySet、keySet和values方法返回的结果进行迭代时,顺序就会反映出来。
  • NavigableMap:继承SortedMap,含有返回特定条件最近匹配的导航方法。
  • HashMap:Map接口基于哈希表的实现,是使用频率最高的用于键值对处理的数据类型。它根据键的hashCode值存储数据,大多数情况下可以直接定位到它的值,特点是访问速度快,遍历顺序不确定,线程不安全,最多允许一个key为null,允许多个value为null。可以用 Collections的synchronizedMap方法使HashMap具有线程安全的能力,或者使用ConcurrentHashMap类。
  • HashTable:Hashtable和HashMap从存储结构和实现来讲有很多相似之处,不同的是它承自Dictionary类,而且是线程安全的,另外Hashtable不允许key和value为null。并发性不如ConcurrentHashMap,因为ConcurrentHashMap引入了分段锁。Hashtable不建议在新代码中使用,不需要线程安全的场合可以使用HashMap,需要线程安全的场合可以使用ConcurrentHashMap。
  • LinkedHashMap: LinkedHashMap继承了HashMap,是Map接口的哈希表和链接列表实现。它维护着一个双重链接列表。此链接列表定义了迭代顺序,该迭代顺序可以是插入顺序或者是访问顺序。
  • WeakedHashMap: 以弱键实现的基于哈希表的Map。在WeakHashMap中,当某个键不再正常使用时,将自动移除其条目。
  • TreeMap : Map接口基于红黑树的实现。

Map

部分顶部注释

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

Map是键值对的映射。map中不能含有重复的key,每个key最多只能映射到一个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接口提供了三种集合视图,允许以键集、值集或键-值映射关系集的形式查看某个映射的内容。map的顺序定义在map的集合视图的迭代器中。一些Map接口的实现,如TreeMap,保证了顺序,而其他的Map实现,如HashMap,则不保证。

定义
public interface Map<K,V>

方法声明

  • Query Operations

int size();
boolean isEmpty();
boolean containsKey(Object key);
boolean containsValue(Object value);
V get(Object key);

  • Modification Operations
  1. V put(K key, V value);
  2. V remove(Object key);
  3. void putAll(Map<? extends K, ? extends V>m);
  4. void clear();
  • Views
  1. Set<K> keySet();
  2. Collection<V> values();
  3. Set<Map.Entry<K, V>> entrySet();
  • Comparison and hashing
  1. boolean equals(Object o);
  2. int hashCode();
  • Defaultable methods
  1. default V getOrDefault(Object key, V defaultValue) {}
  2. default void forEach(BiConsumer<? super K, ? super V> action) {}
  3. default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {//省略方法体}
  4. default V putIfAbsent(K key, V value) {//省略方法体}
  5. default boolean remove(Object key, Object value) {//省略方法体}
  6. default boolean replace(K key, V oldValue, V newValue) {//省略方法体}
  7. default V replace(K key, V value) {//省略方法体}
  8. default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {//省略方法体}
  9. default V computeIfPresent(K key, BiFunction<? super K, ? super V, ? - extends V> remappingFunction) {//省略方法体}
  10. default V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {//省略方法体}
  11. default V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {//省略方法体}

内部接口

interface Entry<K,V> {}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值