结构

  • 继承关系
java.lang.Object
    java.util.AbstractMap<K,V>
        java.util.HashMap<K,V>
  • 实现的接口
Serializable, Cloneable, Map<K,V>
  • 已知的子类
LinkedHashMap, PrinterStateReasons

特点

  • 允许key或者value值为null
  • 非线程安全
  • 无序
  • 读写效率高

性能

  • HashMap的存储结构是数组加链表的形式,get和put方法的时间复杂度都是O(1),所以性能非常高(特殊情况除外,比如很多元素都发生了哈希碰撞)。
  • 有两个参数会影响HashMap的性能,初始容量增长因子
  • 初始容量:就是创建HashMap时哈希表的大小
  • 增长因子:默认为0.75,定义了当HashMap存储的键值对的数量达到多少的时候开始自动增加容量。当HashMap中键值对的数量超过增长因子和当前容量的乘积的时候,哈希表就会重新构建(意思就是重新构建内部数据的结构),从而使哈希表扩张到原来的两倍。

默认的增长因子(0.75)很好的权衡了程序对时间和空间的消耗。

再大一点会减少空间消耗因为比原来更不容易自动扩张),但是会增加对HashMap进行读写等操作时的效率,最常体现在get和set方法上(因为相同大小的哈希表要存比原来更多的数据了,有更大概率发生哈希碰撞,而哈希碰撞的键值都是以链表的方式存储的,查找和插入的效率肯定不如哈希表高)。

所以在设置HashMap初始容量的时候,最好将预计要存入的键值的数量和增长因子考虑在内,从而能够最小化重新构建的操作。
比如说要存入的数据很多的时候,在创建的时候就给他一个足够大的初始容量,而不是让它自己去自动扩张(自动扩张会进行哈希表的重新构建操作)。

构造函数

Constructor and Description
HashMap()
Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).
HashMap(int initialCapacity)
Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75).
HashMap(int initialCapacity, float loadFactor)
Constructs an empty HashMap with the specified initial capacity and load factor.
HashMap(Map<? extends K,? extends V> m)
Constructs a new HashMap with the same mappings as the specified Map.

方法

Modifier and Type

Method and Description

void

clear()

Removes all of the mappings from this map.(删除map中的所有元素)

Object

clone()

Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.(浅克隆,注意是浅克隆)

V

compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)

Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).(JAVA8新特性,不展开讲了,需要自行百度其他教程)

V

computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)

If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.(同上)

V

computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)

If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.(同上)

boolean

containsKey(Object key)

Returns true if this map contains a mapping for the specified key.(是否包含key,时间复杂度约为O(1))

boolean

containsValue(Object value)

Returns true if this map maps one or more keys to the specified value.(是否包含value,时间复杂度约为O(n))

Set<Map.Entry<K,V>>

entrySet()

Returns a Set view of the mappings contained in this map.(以Set的形式表示HashMap中的所有元素,常用于遍历,并且也是《阿里巴巴Java开发手册》中推荐的遍历KV(键值)的方法,下边会介绍)

void

forEach(BiConsumer<? super K,? super V> action)

Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.(JAVA8新特性,对map中的每一个entry执行制定的function)

V

get(Object key)

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.(获取key对应的value,如果不包含key则返回null,时间复杂度约为O(1))

V

getOrDefault(Object key, V defaultValue)

Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.(获取key对应的value,如果不包含key,则返回制定的默认值defaultValue

boolean

isEmpty()

Returns true if this map contains no key-value mappings.(map中是否一个元素都没有)

Set

keySet()

Returns a Set view of the keys contained in this map.(以Set的形式表示HashMap中所有的键)

V

merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)

If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.(JAVA8新特性,不展开讲了,需要自行百度其他教程)

V

put(K key, V value)

Associates the specified value with the specified key in this map.(存入键值,如果key冲突了则会覆盖现有的值)

void

putAll(Map<? extends K,? extends V> m)

Copies all of the mappings from the specified map to this map.(将m中的所有元素放入当前元素中,如果key冲突则会覆盖现有的值)

V

putIfAbsent(K key, V value)

If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.(存入键值,如果key冲突了则会返回现有的值,没有冲突则存入给定的键值并返回null)

V

remove(Object key)

Removes the mapping for the specified key from this map if present.(从map中删除指定的key及其值)

boolean

remove(Object key, Object value)

Removes the entry for the specified key only if it is currently mapped to the specified value.(当map中存在元素其key和value都和指定的相同,则删除)

V

replace(K key, V value)

Replaces the entry for the specified key only if it is currently mapped to some value.(当map中存在指定的key,则用指定的value替换当前value)

boolean

replace(K key, V oldValue, V newValue)

Replaces the entry for the specified key only if currently mapped to the specified value.(当map中存在指定的key,并且其值等于oldValue,则用指定的newValue替换当前oldValue)

void

replaceAll(BiFunction<? super K,? super V,? extends V> function)

Replaces each entry’s value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.(JAVA8新特性,不展开讲了,需要自行百度其他教程)

int

size()

Returns the number of key-value mappings in this map.(返回map中键值对的数量)

Collection

values()

Returns a Collection view of the values contained in this map.(返回此映射中包含的值的collection视图。)