简体   繁体   English

HashSet的 <List<Integer> &gt;时间复杂度

[英]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 . ListhashCode (例如,请参见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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM