jdk1.8 HashMap简介翻译

本文深入探讨了HashMap的性能特点、实现原理及其关键参数,包括初始容量、加载因子等,详细解释了如何优化其使用以提高迭代性能,并强调了在多线程环境下确保线程安全的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

翻译的不好,还请见谅..大笑


 * Hash table based implementation of the <tt>Map</tt> interface.  This

 * implementation provides all of the optional map operations, and permits
 * <tt>null</tt> values and the <tt>null</tt> key.  (The <tt>HashMap</tt>
 * class is roughly equivalent to <tt>Hashtable</tt>, 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.
 * 
 * HashMap基于HashTable,实现了Map接口.该实现提供了所有的map操作,并且允许空的值或键.
 * (HashMap大致上等同于HashTable,但HashMap是非同步的,并且允许空键值)  HashMap不保证
 * 映射的顺序 ,尤其不保证顺序会随着时间的推移而不改变.
 *
 * <p>This implementation provides constant-time performance for the basic
 * operations (<tt>get</tt> and <tt>put</tt>), assuming the hash function
 * disperses the elements properly among the buckets.  Iteration over
 * collection views requires time proportional to the "capacity" of the
 * <tt>HashMap</tt> 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.
 *
 * 如果hash操作将所有元素放在hash桶中合适的地方,那么该实现则可以提供常数时间内执行get或put的操作.
 * 如果想迭代整个集合视图,花费的时间与(HashMap实例的容量(桶的数量)加上它的大小(键值对的数量))成正比.
 * 因此,如果注重迭代性能,切记不要将初始容量设置得太高(或将加载因子设置得太低).
 * 
 *
 * <p>An instance of <tt>HashMap</tt> has two parameters that affect its
 * performance: <i>initial capacity</i> and <i>load factor</i>.  The
 * <i>capacity</i> 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
 * <i>load factor</i> 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 <i>rehashed</i> (that is, internal data
 * structures are rebuilt) so that the hash table has approximately twice the
 * number of buckets.
 *
 * 一个HashMap实例通常有两个参数影响它的性能:1.初始容量;2.加载因子. 
 * 容量:Hashtable中桶的数量,初始容量即哈希表创建时的容量.
 * 加载因子:当hashTable中的数据数量达到什么程度时,才允许自动扩容.
 * 当hashtable中的条目数量超过加载因子和当前容量时,hashTable会rehashed(重构内部数据结构),
 * 重哈希后,hashTable中桶的数量大约是之前的两倍.
 * 
 *
 * <p>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 <tt>HashMap</tt> class, including
 * <tt>get</tt> and <tt>put</tt>).  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).
 * 当设置map的初始容量时,应将map中预期的条目数量和map的加载因子加入考虑范围,
 * 以减少rehashed操作.如果  初始容量>条目总数/加载因子,将不会出现rehashed操作.
 *
 * <p>If many mappings are to be stored in a <tt>HashMap</tt>
 * 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 {@code hashCode()} is a sure way to slow
 * down performance of any hash table. To ameliorate impact, when keys
 * are {@link Comparable}, this class may use comparison order among
 * keys to help break ties.
 *
 * 如果一个HashMap实例中要被排序的数据比较多,则创建HashMap实例时,应该分配足够大的空间给它.
 * 这要比它自己多次rehashed去扩容来得有效率.注意,如果多个key的hashCode是相同的,那么hashTable的
 * 效率绝对是要慢得多的.为了改善这样的影响,当所有的key实现了Comparable接口时,本类会使用比较指令
 * 来减弱这样的影响.
 * 
 * 
 *
 * <p><strong>Note that this implementation is not synchronized.</strong>
 * If multiple threads access a hash map concurrently, and at least one of
 * the threads modifies the map structurally, it <i>must</i> 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.
 *
 *
 * 注意,HashMap不是线程同步的.如果有多个线程并发访问一个hashMap,并且至少有一个
 * 线程对它进行了结构修改操作, 对于这样的情况,你需要在外部进行同步(结构修改指的是增加
 * 或删除一个或多个映射.如果仅仅是改变了一个实例中已存在的键值关系,这不算结构修改.).
 * 这通常需要通过在一些已经封装好的map上使用同步操作来完成.
 *
 * 
 * 
 * If no such object exists, the map should be "wrapped" using the
 * {@link Collections#synchronizedMap Collections.synchronizedMap}
 * method.  This is best done at creation time, to prevent accidental
 * unsynchronized access to the map:<pre>
 *   Map m = Collections.synchronizedMap(new HashMap(...));</pre>
 *   
 * 如果没有这样的对象存在,那么map应该使用Collections.synchronizedMap()包裹起来.
 * 上述操作应在map创建时使用,以防map意外地被多个线程同时访问.
 *
 * <p>The iterators returned by all of this class's "collection view methods"
 * are <i>fail-fast</i>: if the map is structurally modified at any time after
 * the iterator is created, in any way except through the iterator's own
 * <tt>remove</tt> method, the iterator will throw a
 * {@link 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的:在迭代器被创建后,如果map是结构化的,无论在何时,
 * 使用何种方法(除了迭代器本身的remove方法)来修改它,都会抛出ConcurrentModificationException.
 * (也就是HashMap的expectedModCount和modCount对不上的时候会抛出上述异常.)
 * 因此,面对并发修改操作时,迭代器会迅速且清晰地报错.而不是冒着在不确定的时间做不确定的操作的风险.
 * 
 *
 * <p>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 <tt>ConcurrentModificationException</tt> on a best-effort basis.
 * Therefore, it would be wrong to write a program that depended on this
 * exception for its correctness: <i>the fail-fast behavior of iterators
 * should be used only to detect bugs.</i>
 *
 * 注意,迭代器的fail-fast行为是不能保证的.一般来说,保证非同步的同步操作是不太可能的.
 * 在最优基础上,Fail-fast迭代器会抛出ConcurrentModificationException.
 * 因此,写一个为了自身正确性而依赖于这个异常的程序是不对的.迭代器的fail-fast行为应该
 * 只是用来检测bug而已.
 * 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值