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。
HashMap source code view(1)的更多相关文章
- UWP VirtualizedVariableSizedGridView 支持可虚拟化可变大小Item的View(二)
上篇UWP VirtualizedVariableSizedGridView 支持可虚拟化可变大小Item的View(一) 讲到该控件的需要和设计过程. 这篇讲讲开发过程中一些重要问题解决. 1.支持 ...
- Android 自定义View (五)——实践
前言: 前面已经介绍了<Android 自定义 view(四)-- onMeasure 方法理解>,那么这次我们就来小实践下吧 任务: 公司现有两个任务需要我完成 (1)监测液化天然气液压 ...
- Android 自定义 view(四)—— onMeasure 方法理解
前言: 前面我们已经学过<Android 自定义 view(三)-- onDraw 方法理解>,那么接下我们还需要继续去理解自定义view里面的onMeasure 方法 推荐文章: htt ...
- Android 自定义 view(三)—— onDraw 方法理解
前言: 上一篇已经介绍了用自己定义的属性怎么简单定义一个view<Android 自定义view(二) -- attr 使用>,那么接下来我们继续深究自定义view,下一步将要去简单理解自 ...
- Backbone.js学习之Backbone.View(视图)
Backbone.js为复杂WEB应用程序提供模型(models).集合(collections).视图(views)的结构.其中模型用于绑定键值数据和自定义事件:集合附有可枚举函数的丰富API: 视 ...
- 自定义view(二)
这里是自定义view(二),上一篇关于自定义view的一些基本知识,比如说自定义view的步骤.会涉及到哪些函数以及如何实现自定义属性,同时实现了一个很基础的自定义控件,一个自定义的计时器,需要看的人 ...
- 自定义view(一)
为什么标题会是自定义view(一)呢?因为自定义view其实内容很多,变化也很多,所以我会慢慢更新博客,争取多写的有关的东西,同时,如果我以后学到了新的有关于自定义view的东西,我也会及时写出来. ...
- Android开发之漫漫长途 Ⅱ——Activity的显示之Window和View(2)
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
- Android开发之漫漫长途 Ⅱ——Activity的显示之Window和View(1)
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
随机推荐
- error: failed to push some refs to 'https://gitee.com/xxx/xxx'
一开始以为是本地版本和线上的差异 果断先直接pull 之后 还是不对,哎 不瞎搞了 搜... 获得消息: git pull --rebase origin master 原来如此:是缺失了文件
- java中的接口与继承,接口的例子讲解
extends 继承类:implements 实现接口. 简单说: 1.extends是继承父类,只要那个类不是声明为final或者那个类定义为abstract的就能继承, 2.JAVA中不支持多重继 ...
- Cookie的使用(14)
一:cookie的简要介绍: (1)什么是cookie a.cookie是一种客户端的状态管理技术b.当浏览器向服务器发送请求的时候,服务器会将少量的数据以set-cookie消息头的方式发送给浏览器 ...
- hadoop2.7单节点
$ sudo apt-get install ssh$ sudo apt-get install rsync 修改文件 etc/hadoop/hadoop-env.sh # set to the ro ...
- git 命令行
在使用 git 命令行之前需要下载安装软件官方网站:https://git-scm.com/window 或者 mac 等其它版本自行下载 使用方法一:安装后在项目文件夹中右键菜单会有个 Git Ba ...
- Servlet发送邮件遇到的问题SMTPSendFailedException 554
接到通知,一个接收用户请求的邮箱有段时间收不到邮件了.当时想着这么简单的功能,就没有加上日志记录.重写程序后,日志记下的报错是:SMTP的SMTPSendFailedException 554 co ...
- Centos6.5 安装MYSQL 5.5 -5.6.-5.7 一键yum快速安装 ,初始配置
Centos6.5 安装MYSQL 5.5 ---5.6---5.7 一键yum快速安装 ,初始配置 第一步:安装mysql-5.5---- 5.6 ---- 5.7的yum源 [root@sv03 ...
- 看CES 2017上有哪些好玩的物联网设备
2017年国际消费类电子产品展览会(CES)已于昨天在拉斯维加斯正式开幕,多款新一代智能手机和平板电脑亮相本届CES大展,智能家居.穿戴等设备更成为外界普遍关注的焦点.今天笔者将带大家一览CES 20 ...
- Android插件化的兼容性(中):Android P的适配
Android系统的每次版本升级,都会对原有代码进行重构,这就为插件化带来了麻烦. Android P对插件化的影响,主要体现在两方面,一是它重构了H类中Activity相关的逻辑,另一个是它重构了I ...
- webpack之牛刀小试 打包并压缩html、js
1.创建项目文件夹test,在文件夹下创建src文件夹用来存放源码,在src文件夹下创建index.html/index.js两件文件. 我们的最终目的是将这两个文件打包压缩并输出到/test/dis ...