根据 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 中哈希表的同步实现,早于 HashMap
和 HashSet
类。 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/