HashMap source code view(1)

前言

HashMap source code view

类注释

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.
Hash table基于Map接口实现。这个实现提供了所有map的操作选项,并且允许null值和null键。(对于HashMap来说,除了不同步和允许存放null,其他几乎与HashTable一样)这个类不能保证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.
这个类的实现,对于基本的操作(get和put)提供了常数级别的时间复杂度,假设哈希方法能分散各个元素到每个桶中。迭代所有集合元素所需时间与这个HashMap实例的容量(桶的数量)和大小(key-value映射表的数量)成比例。因此,如果迭代性能很重要,那么设置过高的初始容量(或者过低的扩容因子)都会有很大影响。

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的实例有两个参数影响它的性能:初始容量和扩容因子。容量是指哈希表中桶的数量,而初始容量是指当哈希表被创建时的容量。扩容因子是指,在容量自动增加之前哈希表能被装的多满。当哈希表中entry数量超过的扩容因子和当前容量的乘积,那么哈希表将会执行rehash操作(rehash是指内部数据结构重建),以便哈希表有大约两倍桶的数量。

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操作次数。如果初始容量比最大entry数量除以扩容因子还要大,将不会发生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.
如果很多的映射关系将被存放在HashMap的实例中,一开始创建足够大的容量去存放映射关系,这将比它自动rehash扩容来的更有效率。需要注意使用相同hashCode的很多键去存放一定会减慢这个哈希表的性能。为了改善这样的影响,当键是可以比较的,这个类可能使用比较顺序去帮助断开连接。

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,并且至少一个线程在修改这个structurally的结构,它必须在外部使它同步。(一个结构改变的操作是指任意增加或者删除一个或多个映射关系的操作;仅仅改变一个这个哈希表已经包含键对应的值,这样的操作不是一个改变结构的操作)典型的方法是通过一些对象同步来完成的其中自然也包括了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应该使用Collections.synchronizedMap方法。为了避免不同步的对这个map的访问,这个操作最好在创建的时候做

 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 {@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.
迭代器返回这个类的所有“集合视图方法”都是快速失败的:意思是,如果一个map的结构在迭代器创建之后被修改,除了使用迭代器本身的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异常。因此,依赖这个异常处理区编码去保证正确性是不对的:迭代器的快速失败行为应该只是被用于侦测bug。

原文地址:https://www.cnblogs.com/linkstar/p/HashMap-source-code-view1.html

时间: 2024-10-12 07:30:54

HashMap source code view(1)的相关文章

【Java集合源码剖析】HashMap源码剖析(转)

HashMap简介 HashMap是基于哈希表实现的,每一个元素是一个key-value对,其内部通过单链表解决冲突问题,容量不足(超过了阀值)时,同样会自动增长. HashMap是非线程安全的,只是用于单线程环境下,多线程环境下可以采用concurrent并发包下的concurrentHashMap. HashMap 实现了Serializable接口,因此它支持序列化,实现了Cloneable接口,能被克隆. HashMap源码剖析 HashMap的源码如下(加入了比较详细的注释): [ja

HashMap源码阅读(1)- 初始值、数据结构、hash计算

最近有被问及HashMap的相关问题,不得不再阅读源码,刨根问底. 1)初始值 我们平常使用Map的时候,创建的时候都是Map<String,Object> map = new HashMap<String,Object>();那么HashMap的默认大小是多少呢?查看源码,发现这么一段: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 /** * The default initial capacity - MUST be a power of

android自定义View (一)MeasureSpec

A MeasureSpec encapsulates the layout requirements passed from parent to child. Each MeasureSpec represents a requirement for either the width or the height. A MeasureSpec is comprised of a size and a mode. There are three possible modes: UNSPECIFIED

UWP VirtualizedVariableSizedGridView 支持可虚拟化可变大小Item的View(二)

上篇UWP VirtualizedVariableSizedGridView 支持可虚拟化可变大小Item的View(一) 讲到该控件的需要和设计过程. 这篇讲讲开发过程中一些重要问题解决. 1.支持ISupportIncrementalLoading,实现HasMoreItems属性和LoadMoreItemsAsync方法 因为我们上篇里面讲过,需要把源数据分成一个一个的Group作为GirdView的源, 所以LoadMoreItemsAsync方法里面我做了以下的实现: public I

Android 自定义 view(三)&mdash;&mdash; onDraw

前言: 上一篇已经介绍了用自己定义的属性怎么简单定义一个view<Android 自定义view(二) -- attr 使用>,那么接下来我们继续深究自定义view,下一步将要去简单理解自定义view的两个比较重要的方法 onDraw(Canvas canvas) ,在探究 onDraw方法之前,我们必须先深入了解两个类Paint和Canvas .   第一:认识Paint 在探究onDraw之前首先必须要认识两个类,这里给出非常不错的两个资料参考网站,我也是从这里得到想要知道的东西,简单的说

Android Branch and master source code merge(patch)

Environment : Android 4.4.2 merge with Android 4.4.3(with other vendors source code) 1.确定你要merge 到 其他分支的版本,并在服务器测获得具体lable 对应的commit 或者 从build 对应的Repo Manifest 中找到要patch 到目标代码的Commit ID <?xml version="1.0" encoding="UTF-8"?> <

Android自定义控件View(三)组合控件

不少人应该见过小米手机系统音量控制UI,一个圆形带动画效果的音量加减UI,效果很好看.它是怎么实现的呢?这篇博客来揭开它的神秘面纱.先上效果图 相信很多人都知道Android自定义控件的三种方式,Android自定义控件View(一)自绘控件,Android自定义控件View(二)继承控件,还有就是这一节即将学习到的组合控件.我们通过实现圆形音量UI来讲解组合控件的定义和使用. 组合控件 所谓组合控件就是有多个已有的控件组合而成一个复杂的控件.比如上图的音量控件就是一个完美的组合控件.我们来分析

debug运行下报错,但不影响运行ERROR: JDWP Unable to get JNI 1.2 environment, jvm-&gt;GetEnv() return code = -2(转)

eclipse 3.4+jdk1.6 编译正常通过,运行debug模式时报错 ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2JDWP exit error AGENT_ERROR_NO_JNI_ENV(183):  [../../../src/share/back/util.c:820] 查找该错误原因.发现是重定向输出的问题. 以下是网络资料原文 装jdk1.6了把,呵呵- 我也碰到过这

Model(数据模型)-View(视图)-controller(控制器)

1.何为Model Model可以翻译成“数据模型”具体的工作有一下几点: (1)定义数据结构. (2)负责与数据库沟通. (3)从数据库读取或者写入数据. (4)运行预存数据. (5)数据格式验证,对各种数据进行加工处理. 2.何为View View负责所有呈现在用户面前的东西,简单的理解就是输入与输出,输出共组就是呈现在浏览器的界面上.输入的工作就是讲用户的数据传回服务器. 输入: (1)从Controller取得数据,并在用户界面上显示.(2)将Controller传送的数据显示在界面上,