Time-complexity of all 3 collection view iterators for HashMap (myHashMap.entrySet().iterator().next()
and myHashMap.keySet().iterator().next()
and myHashMap.values().iterator().next()
) is well-documented in javadoc, it is O(n+c) for all those 3 iterators (n is number of mappings, c is capacity that is physical number of buckets in hashtable).
But what about respective 3 iterators of 3 respective TreeMap collection views? Nothing is said in official javadoc. What are their complexities? I did look in SE8 source code but I cannot judge from there.
next()
call. – HoiO(1)
. Then, as it's a view (i.e. not a copy of the actual entries), fetching each entry viaiterator.next()
might have some hidden costs. In the case ofTreeMap
, I think it would need to reach the lowest entry, i.e. the leftmost leaf of the tree, which would beO(logN)
because the tree is balanced. Then, for the rest of the entries, it would be just a red-black tree traversal, which would yieldO(1)
for each fetched entry, totallinglogN + N - 1 ~ O(N)
for the whole traversal. – Leavening