12

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.

  • Federico Peralta Schaffner comment to this SO question https://stackoverflow.com/questions/53667320/all-set-and-map-classes-and-all-respective-iterators-next-time-complexity-s implies log(n) for all three TreeMap collection view iterators - as an opinion – driczuketovich Dec 07 '18 at 13:44
  • 1
    @Holger I think he is talking about one `next()` call. – ZhaoGang Dec 07 '18 at 14:01
  • @ZhaoGang exactly. And that’s not what the Javadoc is talking about. – Holger Dec 07 '18 at 14:02
  • For a TreeMap, forming the collection view will be O(n) and iterating over each element will be in total O(n). O(2n) will turn out to be O(n) in essence. Correct? (Considering Full iteration) – A_C Dec 07 '18 at 14:40
  • @Ankur Creating the collection view is just creating object wrappers and setting up some links, so it's `O(1)`. Then, as it's a *view* (i.e. not a copy of the actual entries), fetching each entry via `iterator.next()` might have some hidden costs. In the case of `TreeMap`, I think it would need to reach the lowest entry, i.e. the leftmost leaf of the tree, which would be `O(logN)` because the tree is balanced. Then, for the rest of the entries, it would be just a red-black tree traversal, which would yield `O(1)` for each fetched entry, totalling `logN + N - 1 ~ O(N)` for the whole traversal. – fps Dec 07 '18 at 15:12
  • Well I suspected the same about forming collection view, but not so sure about going to the left most leaf node first. That shouldn’t be required I think. – A_C Dec 07 '18 at 16:01

1 Answers1

4

Try to answer this based on these great comments:

  1. One single next() call has a totally different time complexity compared with the whole iterating process.

  2. TreeMap in Java is based on Red-Black Tree, which is a balanced binary search tree.

Refer to https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html

  1. Iterating a whole TreeMap should have the same time complexity as traversing the Red-Balck Tree(For pre-order in-order or post-order traveral). So time complexity should be O(n) where n is the key(or value, or key-value mapping) count.

  2. For a single next call, we can do it in O(1). If a whole O(n) time complexity is true, this should be trivial.

ZhaoGang
  • 4,491
  • 1
  • 27
  • 39
  • There is no next() in TreeMap. Is there a linked tree map in which we can iterate to next element in O(1)? – Nathan B Feb 20 '22 at 12:54
  • @NathanB By saying `next()` , I mean `iterator.next()` on `entrySet()`, `keySet()`, and `values()`. There should be no linked tree map, we only have `LinkedHashMap`. – ZhaoGang Feb 21 '22 at 02:39
  • The problem with LinkedHashMap is that it is not sorted by the key. – Nathan B Feb 21 '22 at 11:27
  • @NathanB That is true. Are you asking that why the time complexity is `O(1)` for the `iterator.next()` function? – ZhaoGang Feb 24 '22 at 07:23