I am new to Java. I have a question about the time complexity of java iterator().
Set<Integer> set = new HashSet<>();
Iterator<Integer> iter = set.iterator(); ==> (2)
I wanna know what is the time complexity of step 2? It is const time O(1) or it depends on the size of set?
Thanks
Calling iterator() is constant time. It is a method call that returns an Iterator instance on the collection you are calling on. Actually iterating through the collection using the iterator using while (hasNext()) will be O(n)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With