简体   繁体   English

Java 向量 HashSet 中 .add() 的时间复杂度

[英]Time Complexity of .add() in HashSet of Vectors in Java

I have two java HashSets, as below:我有两个 java HashSet,如下所示:

HashSet<Integer> H1 = new HashSet<>();
HashSet<Vector> H2 = new HashSet<>();

As the number of elements in each HashSet grows larger(and assuming some of the elements are unique, some aren't), does the time complexity of adding elements for the second HashSet(*of vectors) change(relative to the second HashSet(*of integers))?随着每个 HashSet 中元素数量的增加(假设一些元素是唯一的,有些不是),为第二个 HashSet(*of vectors) 添加元素的时间复杂度是否发生变化(相对于第二个 HashSet( *整数))? Or does the fact that the HashSet contains Vectors not effect the time complexity?还是 HashSet 包含 Vectors 的事实不会影响时间复杂度?

I understand the time complexity of .add() the first HashSet is(in general) O(1), but could someone clarify for H2 ?我了解.add()的时间复杂度,第一个 HashSet 是(通常)O(1),但有人可以澄清H2吗?

Also, if it were instead, say, a TreeSet of Vectors, how would the time complexity of .add() change from a TreeSet of ints in that case?另外,如果它是 Vectors 的 TreeSet,那么在这种情况下.add()的时间复杂度将如何从 int 的 TreeSet 改变?

Your question makes some false assumptions你的问题做出了一些错误的假设

HashSet<Integer> H1 = new HashSet<>();
HashSet<Vector> H2 = new HashSet<>();

Vector is a legacy synchronized class from Java 1.0. Vector 是 Java 1.0 中遗留的同步类。 You're probably better off using ArrayList.您可能最好使用 ArrayList。 Vector is also a mutable class, meaning that it may not work properly in a HashSet if the Objects it contents are changing Vector 也是一个可变类,这意味着如果它所包含的对象发生变化,它可能无法在 HashSet 中正常工作

As the number of elements in each HashSet grows larger(and assuming some of the elements are unique, some aren't)随着每个 HashSet 中元素数量的增加(假设一些元素是唯一的,有些则不是)

There cannot be non-unique elements in a Set Set 中不能有非唯一元素

As the number of elements in each HashSet grows larger(and assuming some of the elements are unique, some aren't), does the time complexity of adding elements for the second HashSet(*of vectors) change(relative to the second HashSet(*of integers))?随着每个 HashSet 中元素数量的增加(假设一些元素是唯一的,有些不是),为第二个 HashSet(*of vectors) 添加元素的时间复杂度是否发生变化(相对于第二个 HashSet( *整数))? Or does the fact that the HashSet contains Vectors not effect the time complexity?还是 HashSet 包含 Vectors 的事实不会影响时间复杂度?

I understand the time complexity of.add() the first HashSet is(in general) O(1), but could someone clarify for H2?我了解 .add() 的时间复杂度,第一个 HashSet 是(通常)O(1),但有人可以澄清 H2 吗?

Basically, larger lists will change the time complexity - not of the HashSet, but of hashcode() and equals.基本上,更大的列表会改变时间复杂度——不是 HashSet,而是 hashcode() 和 equals。 Also be aware that adding or removing elements from a List after it is added as a key in a hashmap / hashset will generally break the Map.另请注意,在将列表添加为哈希映射/哈希集中的键后,从列表中添加或删除元素通常会破坏映射。

Also, if it were instead, say, a TreeSet of Vectors, how would the time complexity of.add() change from a TreeSet of ints in that case?另外,如果它是 Vectors 的 TreeSet,那么在这种情况下 .add() 的时间复杂度将如何从 int 的 TreeSet 改变?

You can't do this, as Vector does not implement Comparable你不能这样做,因为 Vector 没有实现Comparable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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