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 ...
随机推荐
- 别人的Linux私房菜(16)计划任务
计划任务的种类: at,执行一次就结束的命令 crontab,用户任务循环执行的任务的命令.系统任务编辑/etc/crontab来完成. atd让at生效.crond或cron让crontab生 ...
- 第三周助教工作总结——NWNU李泓毅
本周点评作业数量:第一部分是第一篇博客的剩余部分,给出了共计22份作业的具体分数.第二部分是第二篇博客的已提交部分共计19份作业. 这是我的博客地址:https://www.cnblogs.com/N ...
- MyBatis generator配置 overwrite 文件覆盖失效
工具:IDEA.jdk1.8.mysql 底部有解决方法! pom.xml配置 <plugins> <!--Mybatis自动代码插入--> <plugin> &l ...
- Effective Java -- 使可变性最小化
为了使类成为不可变的,应该遵循以下五条原则: 1. 不要提供任何会下盖对象状态的方法 2. 保证类不会被扩展 3. 使所有的域都是final的 4. 使所有的域都成为私有的 5. 确保对于任何可变组件 ...
- Java性能优化的50个细节(珍藏版)
原文地址:https://www.toutiao.com/i6595499804082569742/ 在JAVA程序中,性能问题的大部分原因并不在于JAVA语言,而是程序本身.养成良好的编码习惯非常重 ...
- 第44章:MongoDB-集群--Sharding(分片)--分片的片键选择
①片键选择的重要性 所谓片键,就是用来拆分数据的字段,通常为1-2个字段,由于片键一旦确定,并已经分片过后,基本上就不可能再修改片键了,因此初期设计和选择就非常重要了 ②片键规则 1:不可以是数组 2 ...
- spring aop 的代理工厂
参考 https://docs.spring.io/spring/docs/4.3.11.RELEASE/spring-framework-reference/htmlsingle/#aop-unde ...
- pageHelper的使用步骤,省略sql语句中的limit
1.引架包.注意版本问题 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId& ...
- EF学习笔记(七):读取关联数据
总目录:ASP.NET MVC5 及 EF6 学习笔记 - (目录整理) 本篇参考原文链接:Reading Related Data 本章主要讲述加载显示关联数据: 数据加载分为以下三种 Lazy l ...
- beautifulsoup爬取糗事百科
# _*_ coding:utf-8 _*_ import urllib2 from bs4 import BeautifulSoup user_agent = "Mozilla/5.0 ( ...