Java 集合框架(Map 接口)

Map 接口简介

对于 Map 接口,Java的官方文档是这样介绍的:

A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value. It models the mathematical function abstraction. The Map interface includes methods for basic operations (such as put, get, remove, containsKey, containsValue, size, and empty), bulk operations (such as putAll and clear), and collection views (such as keySet, entrySet, and values).

简单来说,讲了以下几点:

  • Map 是一个键值对映射的对象,是模拟数学函数的实现
  • 一个 map 不能包含重复键,每个键可以映射多个值
  • Map 接口包含了以下操作:
    • 基础操作:putgetremovecontainsKeycontainsValuesizeisEmpty
    • 批量操作:putAllclear
    • 集合视图:keySetvaluesentrySet

然后讲了下关于 Map 的实现:

The Java platform contains three general-purpose Map implementations: HashMap, TreeMap, and LinkedHashMap. Their behavior and performance are precisely analogous to HashSet, TreeSet, and LinkedHashSet, as described in The Set Interface section.

这里讲的很简单,概括下就几句话:

  • Map 接口包含了3个通用实现(general-purpose implements):HashMapTreeMapLinkedHashMap
  • 这3个通用实现的行为和表现完全类似于 HashSetTreeSetLinkedHashSet

Map 接口相关操作

  • 基础操作(Basic Operations)
V put(K key, V value);
V get(Object key);
V remove(Object key);
boolean containsKey(Object key);
boolean containsValue(Object value);
int size();
boolean isEmpty();
  • 批量操作(Bulk Operations)
void putAll(Map<? extends K, ? extends V> m);
void clear();
  • 集合视图(Collection Views )
Set<K> keySet();
Collection<V> values();
Set<Map.Entry<K, V>> entrySet();

更多参考:The Map Interface (The Java™ Tutorials > Collections > Interfaces)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容