哈希 table 和 javascript 中的哈希映射

Hash table and Hash map in javascript

哈希表 && 哈希图的内部表示是,

在Java中,hashtablehashmap是在synchronous/asynchronous操作上区分的,否则内部表示一样。

Java脚本对象文字表示法,

var obj = {
           e1: 1,
           e2: 2,
           e3: 3  
         };

可以直接作为hashtable和hashmap使用,内部有散列函数。对象字面量以字符串或符号作为键。

ES6 还引入了 window.Map 以任意值作为键。

1) 哈希表和哈希图的上述内部表示是否正确? (or) hashtable 和 hashmap 的内部表示有区别吗?

2) Javascript object literal 是否为 hashtable/hashmap 提供 O(1) 计算而没有任何冲突?

您的图片是 一种 实现 hash table data structure - there are others. Your image is an example of "separate chaining", "open addressing" is another common strategy. As of Java 7 both Hashtable and HashMap used a form of chaining (look at their Entry classes), however Java 8 introduced a massive rewrite of HashMap (but not Hashtable 的方法,因为它是遗留 class) 使用树结构而不是链表它的链条。关键是两个 classes 使用的确切算法是一个实现细节,可能会在不同版本之间发生变化。

"HashMap"和"Hashtable"只是JDK定义的class名称,它们不一定对应于特定的哈希算法。 Java脚本没有单独的 "HashMap" 和 "Hashtable" 概念,因为它们不需要它们。 Java 需要创建一个单独的 HashMap class 因为 Hashtable 合同有问题并且无法安全地更正。

所以,回答你的问题:

1) 排序;它不能正确表示 Java 8 的 HashMap,但在概念上相距不远。 Hashtable 仍然使用这个算法,但你几乎不应该使用 Hashtable.

2) 正如您所说,Java脚本对象在幕后使用散列 table 数据结构。这确实(通常)提供 O(1) 字段访问,但与任何哈希 table 实现一样,它必须处理冲突的可能性。正如链接到 .

的问题@overexchange 中所讨论的