简体   繁体   English

Java 8 HashTable与HashMap冲突处理

[英]Java 8 HashTable vs HashMap Collision Handling

I want some clarification about the differences between hashtable and hashmap in Java 8. 我想澄清一下Java 8中的hashtable和hashmap之间的区别。

HashTable to my knowledge functions similarly to a HashMap but is threadsafe, and doesn't allow null keys or values. 据我所知,HashTable的功能类似于HashMap,但具有线程安全性,并且不允许空键或空值。 I am aware that Java 8 updates the HashMap class so that when there's a collision, rather than create a linked list of items with the same hash code in the bucket, it creates a tree. 我知道Java 8更新了HashMap类,以便在发生冲突时,而不是创建存储桶中具有相同哈希码的项目的链表,而是创建树。

Is this also the case with hashtable? 哈希表也是如此吗? And is the tree the default storage method even before a collision? 甚至在发生碰撞之前,树是默认的存储方法吗? (As in, when the first item to fit into a bucket is placed there, it's just a tree root with no branches.) (例如,将第一个可放入桶中的物品放在那里时,它只是没有分支的树根。)

Also, how does java ensure that a hashtable is threadsafe? 另外,java如何确保哈希表是线程安全的? Does it create a que when two threads try to concurrently access a piece of data? 当两个线程尝试并发访问数据时,是否会创建队列?

Is this also the case with hashtable? 哈希表也是如此吗?

No. 没有。

And is the tree the default storage method even before a collision? 甚至在发生碰撞之前,树是默认的存储方法吗? (As in, when the first item to fit into a bucket is placed there, it's just a tree root with no branches.) (例如,将第一个可放入桶中的物品放在那里时,它只是没有分支的树根。)

No. The HashMap class has a constant named TREEIFY_THRESHOLD (value 8). 号的HashMap类有一个名为恒定TREEIFY_THRESHOLD (值8)。

Javadoc says "The bin count threshold for using a tree rather than list for a bin. Bins are converted to trees when adding an element to a bin with at least this many nodes" . Javadoc说: “使用树而不是容器列表的容器计数阈值。将元素添加到具有至少这么多节点的容器中时,容器将转换为树”

Also, how does java ensure that a hashtable is threadsafe? 另外,java如何确保哈希表是线程安全的?

The javadoc of Hashtable explicit says: "Unlike the new collection implementations, Hashtable is synchronized " . Hashtable的javadoc明确表示: “与新的集合实现不同,Hashtable是同步的

Does it create a que when two threads try to concurrently access a piece of data? 当两个线程尝试并发访问数据时,是否会创建队列?

No. 没有。

This answer is going to be very specific to the current implementation found in the JDK so keep that in mind. 这个答案将非常特定于JDK中的当前实现,因此请记住这一点。

  1. I am aware that Java 8 updates the HashMap class so that when there's a collision, rather than create a linked list of items with the same hash code in the bucket, it creates a tree. 我知道Java 8更新了HashMap类,以便在发生冲突时,而不是创建存储桶中具有相同哈希码的项目的链表,而是创建树。

    Is this also the case with hashtable? 哈希表也是如此吗?

No. The Hashtable is only ever going to be a table of linked lists. 不会。哈希表永远只会是链接列表的表。 Java devs didn't update the Hashtable for this use case. Java开发人员没有为此用例更新Hashtable。 This should also make it more clear that you probably shouldn't be using Hashtable anyway. 这也应该使您更清楚地知道您可能不应该使用Hashtable。

  1. And is the tree the default storage method even before a collision? 甚至在发生碰撞之前,树是默认的存储方法吗?

Again this is in Java 8, as of today, but, no it's not the default behavior. 再次重申,这是Java 8,直到今天,但是,这不是默认行为。 Once the bucket entry hits 8 linked elements the HashMap turns that linked list into a binary tree. 一旦存储桶条目达到8个链接元素 ,HashMap就会将该链接列表转换为二叉树。

  1. Also, how does java ensure that a hashtable is threadsafe? 另外,java如何确保哈希表是线程安全的? Does it create a que when two threads try to concurrently access a piece of data? 当两个线程尝试并发访问数据时,是否会创建队列?

By making every method synchronized, meaning, each thread will queue up on the method while another thread is currently accessing any method of the Hashtable. 通过使每个方法同步(即,每个线程将在该方法上排队),而另一个线程当前正在访问Hashtable的任何方法。 This causes quite a few issues if you want to do things like atomic puts, or atomic computes. 如果您要执行原子投放或原子计算之类的操作,则会导致很多问题。

If you wanted a thread safe map HashMap, always use ConcurrentHashMap, never Hashtable. 如果您想要线程安全的映射HashMap,请始终使用ConcurrentHashMap,而不要使用Hashtable。

From Hashtable's Java Docs : 从Hashtable的Java文档

In the case of a "hash collision", a single bucket stores multiple entries, which must be searched sequentially 如果发生“哈希冲突”,则单个存储桶会存储多个条目,这些条目必须按顺序搜索

So the answer for the first question is "No". 因此,第一个问题的答案是“否”。 It does not create any tree. 它不会创建任何树。

As to threadsafe mechanism, Hashtable uses simple synchronization on each read\\write method. 关于线程安全机制,Hashtable在每个读写方法上使用简单的同步。 If you're concerned about performance and thread safety, you'd better look at ConcurrentHashMap . 如果您担心性能和线程安全性,最好查看ConcurrentHashMap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM