HashMap源码注释翻译

/*
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 */

package java.util;

import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;


 * 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 实现了Map接口(基于哈希表)。HashMap提供了所有可选的map操作,允许
 * key和values为null。HashMap和Hashtable类似,不同之处在于HashMap是非同步的,允许
 * 空值。HashMap不能保证map的顺序;特别指出,不能保证这个顺序一直保持不变。
 *
 * <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.
 *
 * 译:假设哈希函数适当地打乱桶中的元素为前提,HashMap为get/put基本操作提供了恒定
 * 的时间性能。(中间这句还不能准确的翻译,大概意思是迭代的性能依赖于map的容量和键值
 * 对的数量,进而推导出下面这句)因此,如果迭代性能要求高的话,不能设置容量太大或者
 * 装载因子太低变得非常重要的。
 *
 * <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实例有两个参数影响它的性能:初始容量和装载因子。容量是哈希表中桶的
 * 数量,初始容量是哈希表创建时的容量。装载因子用来衡量HashMap满的程度,就是决定hash
 * 表中用掉多少容量时,进行自动扩充容量的因子.当哈希表中entry的数量超过装载因子和当前
 * 容量的乘积时,哈希表内部结构将会重新构建,使得哈希表容量约是桶数量的两倍。
 *
 * <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中entry的预期数量
 * 和装载因子,以便使rehash操作最小。如果初始容量大于最大条目数除以装载因子
 * ,rehash操作将不会发生。
 *
 * <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.
 *
 * 译:如果你预计有很多元素将存入map,那么主动设置capacity大一点,有助于减少
 * rehashing的次数,从而提高性能。如果很多关键字的哈希值相同,会降低哈希表的
 * 性能。为了降低这个影响,当键支持java.lang.Comparable时,HashMap可以对
 * 关键字做次排序。
 *
 * <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是不同步的。如果多个线程同时访问哈希映射,并且至少一个
 * 线程在结构上修改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>
 *
 * 译:如果没有这样的对象,那么需要用synchronizedMap来封装.
 *
 * <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在iterator创建之后
 * 结构进行了改变(除了通过迭代器自身remove方法)则会抛出ConcurrentModificationException。
 * 因此,在面临并发修改时,迭代器会快速而干净地失败,远比不管情况,不确定操作带来风险要好。
 *
 * <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机制.
 * 抛出ConcurrentModificationException只能说是尽最大努力.
 * 因此在自己的代码里不要依赖抛出这个异常.它只用于发现bug!
 *
 * <p>This class is a member of the
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
 * Java Collections Framework</a>.
 *
 * 译:地址:http://docs.oracle.com/javase/8/docs/technotes/guides/collections/index.html
 * 这里可以获得官方对容器的详细解释,值得一读.
 *

/**
 * @param <K> the type of keys maintained by this map
 * @param <V> the type of mapped values
 *
 * @author  Doug Lea
 * @author  Josh Bloch
 * @author  Arthur van Hoff
 * @author  Neal Gafter
 * @see     Object#hashCode()
 * @see     Collection
 * @see     Map
 * @see     TreeMap
 * @see     Hashtable
 * @since   1.2
 */
public class HashMap<K,V> extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable {

    private static final long serialVersionUID = 362498820763181265L;


}

翻译了开头注释,感觉很吃力。如果不懂里面的原理,正文中的注释翻译比较难。未完待续

翻译参考文章:https://www.cnblogs.com/killbug/p/7679043.html(有部分翻译参考自这篇文章)

HashMap原理:https://www.cnblogs.com/chengxiao/p/6059914.html(可参考这篇文章,感觉图文并茂,易懂)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值