java - HashSet使用Hash Table作为数据结构,使用HashMap作为集合?内存角度数组不推荐使用?

标签 java arraylist collections hashmap

根据 this

Underlying data structure for HashSet is hashtable.

但我还了解到,HashSet 内部使用 HashMap 来避免重复值,而重复值又在内部使用存储桶数组和 LinkedList(在 Java 8 中被树替换)

那么说HashSet使用HashTable作为数据结构,HashMap作为集合,这样说对吗?

TreeSet implements the SortedSet interface so duplicate values are not allowed.

这是否意味着 TreeSet 内部不使用 HashMap,而 HashSet 使用 HashMap 来避免重复值?LinkedHashSet 内部是否使用 HashMap?

根据 this

Memory point of view arrays are not recommended to use.

为什么? 根据我之前读到的内容-

Since ArrayList can’t be created for primitive data types, members of ArrayList are always references to objects at different memory locations (See this for details). Therefore in ArrayList, the actual objects are never stored at contiguous locations. References of the actual objects are stored at contiguous locations. In array, it depends whether the arrays is of primitive type or object type. In case of primitive types, actual values are contiguous locations, but in case of objects, allocation is similar to ArrayList.

最佳答案

小写哈希表(一般定义的数据结构)与 java Hashtable 之间存在差异。类,它是 Java 中哈希表的同步实现,早于 HashMapHashSet 类。 HashSet 根本不使用Hashtable 类。相反,它使用(同样是小写 h)哈希表数据结构,该结构是使用 HashMap 实现的。 Hashtable 应该很少在现代代码中使用。根据javadocs for Hashtable :

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.

至于您的其他问题:

  • TreeSet 使用与哈希表完全不同的数据结构,称为红黑树。参见例如this detailed answer概述 HashSet 和 TreeSet 之间的差异。
  • 我不确定“内存视角数组”中的引用是什么意思。该网站写得不好。

关于java - HashSet使用Hash Table作为数据结构,使用HashMap作为集合?内存角度数组不推荐使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50743760/

相关文章:

java - 简单来说什么是 ArrayList 以及如何使用它?

java - 以随机顺序对arraylist进行排序

java - 如何对两个对象使用集合方法(removeAll()和retainAll())

java - 为什么 Collections#shuffle 不使用 ThreadLocalRandom?

java - 自定义 HashMap 代码问题

java - Servlet 过滤器 url-mapping/* 不适用于 404 错误

java - android中的随机图像

java - 无法从站点获取服务器证书

java - 如何在 Java 的 ArrayList 类中使用 add()

java - 如何将此代码片段变形为逻辑取决于索引值的 Java 8?