[英]HashSet<List<Integer>> time complexity
I know HashSet
has both lookup/add time complexity of O(1) . 我知道HashSet
同时具有O(1)的查找/添加时间复杂度。 But I was worried if it has the same time complexity if the Set
holds List
, for me it's hard to believe it would only takes O(1), how would we do hash on a List? 但是我担心如果Set
拥有List
,它是否具有相同的时间复杂性,对我来说很难相信它只会占用O(1),我们将如何对List进行哈希处理?
Thanks 谢谢
hashCode
of List
s (for example, see the implementation of AbstractList
) is a function of all the elements of the List
. List
的hashCode
(例如,请参见AbstractList
的实现)是List
的所有元素的函数。 The same is true for the equals
implementation. 对于equals
实现,也是如此。
Therefore lookup/add in a Set<List<SomeType>>
will take O(k)
where k
is the size of the List
you are looking for/adding. 因此,在Set<List<SomeType>>
查找/添加将占用O(k)
,其中k
是您要查找/添加的List
的大小。 However, since k
doesn't depend on the number of elements in the Set
, the lookup time would still be a constant function of the number of elements of the Set
, so you can still say lookup takes O(1)
, since the running time of lookup/add in a Collection
is calculated as a function of the number of elements in the Collection
, not the size of individual elements. 但是,由于k
不取决于Set
中元素的数量,因此查找时间仍将是Set
中元素数量的常数,因此您仍可以说lookup需要O(1)
,因为运行查找的时间/在添加一个Collection
被计算为在元件的数量的函数的Collection
,而不是单个元件的尺寸。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.