hashmap in java ,的结构
HashMap (Java SE 11 & JDK 11 )
since : 1.2
Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
基于哈希表实现的Map接口。该实现提供所有可选的映射操作,并允许空值和空键。(HashMap类大致等同于Hashtable,但它是非同步的且允许空值。)此类不保证映射的顺序;尤其不保证顺序会随时间推移保持不变。
This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets. Iteration over collection views requires time proportional to the "capacity" of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.
该实现为基本操作(get和put)提供了恒定时间性能,前提是哈希函数将元素适当地分散在各个存储桶中。遍历集合视图所需时间与HashMap实例的"容量"(存储桶数量)加上其大小(键值映射数量)成正比。因此,如果迭代性能很重要,则不要将初始容量设置过高(或负载因子过低)非常重要。
An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.
HashMap 的实例有两个参数会影响其性能:初始容量和负载因子。容量是哈希表中桶的数量,初始容量就是哈希表创建时的容量。负载因子是哈希表在其容量自动增加之前可以多满的一种度量。当哈希表中的条目数超过负载因子与当前容量的乘积时,哈希表将被重新哈希(即内部数据结构将被重建),从而使哈希表具有大约两倍的桶数。
As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put). The expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur.
通常来说,默认负载因子(0.75)在时间成本和空间成本之间提供了良好的折中方案。较高的负载因子会减少空间开销,但会增加查找成本(体现在HashMap类的大多数操作中,包括get和put)。在设置初始容量时,应考虑映射中的预期条目数及其负载因子,以尽量减少rehash操作次数。如果初始容量大于最大条目数除以负载因子,则永远不会发生rehash操作。
If many mappings are to be stored in a HashMap instance, creating it with a sufficiently large capacity will allow the mappings to be stored more efficiently than letting it perform automatic rehashing as needed to grow the table. Note that using many keys with the same hashCode() is a sure way to slow down performance of any hash table. To ameliorate impact, when keys are Comparable, this class may use comparison order among keys to help break ties.
如果将许多映射关系存储在HashMap实例中,创建时设置足够大的初始容量,会比让哈希表在需要时自动扩容重新哈希更高效。需要注意的是,若大量键具有相同的hashCode()值,必然会降低哈希表的性能。为缓解此影响,当键实现了Comparable接口时,该类可能会使用键之间的比较顺序来辅助解决哈希冲突。
Note that this implementation is not synchronized. If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:
请注意,此实现不是同步的。如果多个线程并发访问哈希映射,并且至少有一个线程在结构上修改了该映射,则必须从外部进行同步。(结构修改是指添加或删除一个或多个映射的任何操作;仅更改实例已包含的键所关联的值不属于结构修改。)通常可以通过对自然封装该映射的某个对象进行同步来实现。如果不存在这样的对象,则应使用 Collections.synchronizedMap 方法对该映射进行"包装"。最好在创建时完成此操作,以防止意外不同步访问该映射:
Map m = Collections.synchronizedMap(new HashMap(...));
The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
该类的所有“集合视图方法”返回的迭代器都是快速失败(fail-fast)的:如果在创建迭代器后的任意时刻对映射进行结构性修改(除非通过迭代器自身的remove方法),迭代器将抛出ConcurrentModificationException异常。因此,面对并发修改时,迭代器会快速且明确地失败,而非冒着在未来不确定时间出现任意、非确定性行为的风险。
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
需要注意的是,迭代器的快速失败行为无法得到绝对保证,因为在存在非同步并发修改的情况下,通常不可能做出任何硬性保证。快速失败迭代器会尽最大努力抛出ConcurrentModificationException。因此,若编写依赖此异常来保证正确性的程序是错误的:迭代器的快速失败行为应仅用于检测程序错误。
This class is a member of the Java Collections Framework.
-
Constructors Constructor Description HashMap()Constructs an empty
HashMapwith the default initial capacity (16) and the default load factor (0.75).HashMap(int initialCapacity)Constructs an empty
HashMapwith the specified initial capacity and the default load factor (0.75).HashMap(int initialCapacity, float loadFactor)Constructs an empty
HashMapwith the specified initial capacity and load factor.HashMap(Map<? extends K,? extends V> m)Constructs a new
HashMapwith the same mappings as the specifiedMap.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all of the mappings from this map.
Objectclone()Returns a shallow copy of this
HashMapinstance: the keys and values themselves are not cloned.Vcompute(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
nullif there is no current mapping).VcomputeIfAbsent(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 unlessnull.VcomputeIfPresent(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.
booleancontainsKey(Object key)Returns
trueif this map contains a mapping for the specified key.booleancontainsValue(Object value)Returns
trueif this map maps one or more keys to the specified value.Set<Map.Entry<K,V>>entrySet()Returns a Set view of the mappings contained in this map.
Vget(Object key)Returns the value to which the specified key is mapped, or
nullif this map contains no mapping for the key.booleanisEmpty()Returns
trueif this map contains no key-value mappings.Set<K>keySet()Returns a Set view of the keys contained in this map.
Vmerge(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.
Vput(K key, V value)Associates the specified value with the specified key in this map.
voidputAll(Map<? extends K,? extends V> m)Copies all of the mappings from the specified map to this map.
Vremove(Object key)Removes the mapping for the specified key from this map if present.
intsize()Returns the number of key-value mappings in this map.
Collection<V>values()Returns a Collection view of the values contained in this map.
参考美团 技术BLOG:
PUT

扩容

更多推荐
所有评论(0)