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 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.
 *
 * 译:HashMap 实现了Map接口(基于哈希表)。HashMap提供了所有可选的map操作,允许
 * key和values为null。HashMap和Hashtable类似,不同之处在于HashMap是非同步的,允许
 * 空值。HashMap不能保证map的顺序;特别指出,不能保证这个顺序一直保持不变。
 *
 * 

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. * * 译:假设哈希函数适当地打乱桶中的元素为前提,HashMap为get/put基本操作提供了恒定 * 的时间性能。(中间这句还不能准确的翻译,大概意思是迭代的性能依赖于map的容量和键值 * 对的数量,进而推导出下面这句)因此,如果迭代性能要求高的话,不能设置容量太大或者 * 装载因子太低变得非常重要的。 * *

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实例有两个参数影响它的性能:初始容量和装载因子。容量是哈希表中桶的 * 数量,初始容量是哈希表创建时的容量。装载因子用来衡量HashMap满的程度,就是决定hash * 表中用掉多少容量时,进行自动扩充容量的因子.当哈希表中entry的数量超过装载因子和当前 * 容量的乘积时,哈希表内部结构将会重新构建,使得哈希表容量约是桶数量的两倍。 * *

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)。当设定初始容量时,应当考虑map中entry的预期数量 * 和装载因子,以便使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 {@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可以对 * 关键字做次排序。 * *

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. * * 译:值得注意的是,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:

 *   Map m = Collections.synchronizedMap(new HashMap(...));
* * 译:如果没有这样的对象,那么需要用synchronizedMap来封装. * *

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 * {@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。 * 因此,在面临并发修改时,迭代器会快速而干净地失败,远比不管情况,不确定操作带来风险要好。 * *

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. * * 译:注意,一般不可能保证在一个线程不安全的操作中这种fail-fast机制. * 抛出ConcurrentModificationException只能说是尽最大努力. * 因此在自己的代码里不要依赖抛出这个异常.它只用于发现bug! * *

This class is a member of the * * Java Collections Framework. * * 译:地址:http://docs.oracle.com/javase/8/docs/technotes/guides/collections/index.html * 这里可以获得官方对容器的详细解释,值得一读. * /** * @param the type of keys maintained by this map * @param 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 extends AbstractMap implements Map, 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(可参考这篇文章,感觉图文并茂,易懂)

你可能感兴趣的:(Java)