看到类似代码片段
Set<Record> instances = new HashSet<Record>();我想知道Hashset是否是一种特殊的集合。它们之间有什么区别吗?
I am wondering if Hashset is a special kind of set. Any difference between them?
推荐答案A 集合代表一个通用的集合价值观。 TreeSet 是一个元素排序(因而有序)的集合, HashSet 是一个元素集合是不排序或订购。
A Set represents a generic "set of values". A TreeSet is a set where the elements are sorted (and thus ordered), a HashSet is a set where the elements are not sorted or ordered.
A HashSet 通常比a快很多 TreeSet 。
A HashSet is typically a lot faster than a TreeSet.
A TreeSet 通常实现为红黑树(见 en.wikipedia/wiki/Red-black_tree - 我没有验证sun / oracle的 TreeSet 的实际实现,而 HashSet 使用 Object.hashCode()在数组中创建索引。红黑树的访问时间为 O(log(n)),而 HashSet 的访问时间范围为恒定时间到最坏的情况(每个项目都有相同的hashCode),你可以在线性搜索时间 O(n)。
A TreeSet is typically implemented as a red-black tree (See en.wikipedia/wiki/Red-black_tree - I've not validated the actual implementation of sun/oracle's TreeSet), whereas a HashSet uses Object.hashCode() to create an index in an array. Access time for a red-black tree is O(log(n)) whereas access time for a HashSet ranges from constant-time to the worst case (every item has the same hashCode) where you can have a linear search time O(n).
更多推荐
HashSet和Set之间有什么区别?
发布评论