HashMap (Java Platform SE 8)

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.

Hashtable哈希表基于Map映射接口的实现。这个实现提供了所有可能的映射操作,并且允许空值和空键。(HashMap类和Hashtable类大致相同,除了异步和允许空键值。)这个类不保证映射的顺序;尤其是不保证会保持某个顺序。

 

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实例的“容量”和集合的大小成正比。因此,如果注重遍历性能就不要把初始容量设置得太高(或是把加载因子设得太低)。

 

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实例有两个参数影响到它的性能:初始容量和加载因子。容量是指哈希表中桶的个数,初始容量就是哈希表创建时的容量。加载因子是一个哈希表满度的允许值,超过这个值哈希表的容量将自动增加。当哈希表的存储量达到了加载因子和当前容量的乘积时,哈希表将会重新哈希(内部数据结构将改变),之后哈希表将有近两倍的容量。

 

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,这是时间和空间的一个权衡。这个数高一些将会节省空间开支但会增加查找消耗(考虑到哈希表大部分操作是取和存)。在设置初始容量时应该考虑到映射的预计数量和加载因子,以尽可能的减少重新哈希的次数。如果初始容量大于预期存储数量除以加载因子,那么将不会发生重新哈希操作。

 

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 hashCode() is a sure way to slow down performance of any hash table. To ameliorate impact, when keys are Comparable, this class may use comparison order among keys to help break ties.

如果要在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. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the 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 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机制: 当迭代器被创建后映射关系发生结构性改变,除了迭代器自有的remove方法,任何操作都将导致迭代器抛出异常。因此面对并发修改,迭代器的快速失败行为好过冒在未来某一时间发生随机的不确定行为的风险。

 

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.

This class is a member of the Java Collections Framework.

迭代器的快速失败行为无法得到保证,在异步并发修改的当场保证会触发是不可能的。快速失败操作会尽最大努力抛出异常。因此,为提高此类操作的正确性而编写一个依赖于此异常的程序是错误的:迭代器的快速失败操作应仅用于查bug。这个类是Java集合框架的成员。

 

Hashtable (Java Platform SE 8)

This class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value.

不支持空键或空值。

 

To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.

为了实现存取,作为键的对象必须执行hashCode函数和equals函数。

 

As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.

hashtable类被加装成Map接口,成为Java集合框架的一员。不像新的集合实现,Hashtable是同步的。如果不需要线程安全,建议使用HashMap代替Hashtable。如果需要满足线程安全和高并发,建议使用ConcurrentHashMap代替Hashtable。

 

HashSet (Java Platform SE 8)

This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.

这个类基于哈希表实现了Set接口(实际上是一个HashMap实例)。允许空元素。

 

Note that this implementation is not synchronized.

这个实现是异步的。

 

 

哈希冲突:不同的对象出现相同的哈希值,它们在数组中存储的位置出现了冲突。

1.开放定址法:线性探测法、平方探测法、双散列;

2.二次再散列法:指第一次散列产生哈希地址冲突,为了解决冲突,采用另外的散列函数或者对冲突结果进行处理的方法;

3.拉链法:冲突成链;

4.公共溢出区:冲突存储在公共区域;

 

posted on 2020-08-10 01:49  Mr.Winter  阅读(170)  评论(0编辑  收藏  举报