9

I am trying to implement a plane sweep algorithm and for this I need to know the time complexity of java.util.HashMap class' keySet() method. I suspect that it is O(n log n). Am I correct?

Point of clarification: I am talking about the time complexity of the keySet() method; iterating through the returned Set will take obviously O(n) time.

2
  • 1
    I don't think walking through the set takes 'obviously' O(n) time. The set may not be actually generate. It maybe just a object that can be used to iterate. And, what's the n here? The items or buckets? Commented Sep 3, 2012 at 6:06
  • here n is the number of entries in the hashmap. If the set has n elelments, to iterate over the set of n elements the time complexity will be O(n). Commented Nov 1, 2012 at 22:05

6 Answers 6

28

Getting the keyset is O(1) and cheap. This is because HashMap.keyset() returns the actual KeySet object associated with the HashMap.

The returned Set is not a copy of the keys, but a wrapper for the actual HashMap's state. Indeed, if you update the set you can actually change the HashMap's state; e.g. calling clear() on the set will clear the HashMap!


... iterating through the returned Set will take obviously O(n) time.

Actually that is not always true:

  • It is true for a HashMap is created using new HashMap<>(). The worst case is to have all N keys land in the same hash chain. However if the map has grown naturally, there will still be N entries and O(N) slots in the hash array. Thus iterating the entry set will involve O(N) operations.

  • It is false if the HashMap is created with new HashMap<>(capacity) and a singularly bad (too large) capacity estimate. Then it will take O(Cap) + O(N) operations to iterate the entry set. If we treat Cap as a variable, that is O(max(Cap, N)), which could be worse than O(N).

There is an escape clause though. Since capacity is an int in the current HashMap API, the upper bound for Cap is 231. So for really large values of Cap and N, the complexity is O(N).

On the other hand, N is limited by the amount of memory available and in practice you need a heap in the order of 238 bytes (256GBytes) for N to exceed the largest possible Cap value. And for a map that size, you would be better off using a hashtable implementation tuned for huge maps. Or not using an excessively large capacity estimate!

Sign up to request clarification or add additional context in comments.

1 Comment

(Note: the 2nd part of this Answer was originally a separate Answer.)
5

Surely it would be O(1). All that it is doing is returning a wrapper object on the HashMap.

If you are talking about walking over the keyset, then this is O(n), since each next() call is O(1), and this needs to be performed n times.

1 Comment

O(n) is not completely accurate, per my answer (the time complexity of iteration is dependent on both the map size as well as its capacity, though the capacity piece shouldn't change the iteration time complexity unless the map is poorly configured for its data).
2

This should be doable in O(n) time... A hash map is usually implemented as a large bucket array, the bucket's size is (usually) directly proportional to the size of the hash map. In order to retrieve the key set, the bucket must be iterated through, and for each set item, the key must be retrieved (either through an intermediate collection or an iterator with direct access to the bucket)...

**EDIT: As others have pointed out, the actual keyset() method will run in O(1) time, however, iterating over the keyset or transferring it to a dedicated collection will be an O(n) operation. Not quite sure which one you are looking for **

2 Comments

O(n) is not completely accurate, per my answer (the time complexity of iteration is dependent on both the map size as well as its capacity, though the capacity piece shouldn't change the iteration time complexity unless the map is poorly configured for its data).
@M.Justin - And per my answer ... from 9 months earlier.
2

According to official Java documentation, The time complexity of keySet() method is O(1). Because when anything changes happens in map at any time those all change related to key happens for keySet() method also.

Think like there is a static ArrayList which is gobally and all changes are made when some key is added or removed.

But in interview or if you are implementing hashmap from scratch then You can do keySet() time complexity O(1) and if you are finding all those key's using some traversal then its time complexity will be O(n).

For More information read Java official hashmap documention. Java Hashmap

Comments

0

Java collections have a lot of space and thus don't take much time. That method is, I believe, O(1). The collection is just sitting there.

Comments

0

To address the "iterating through the returned Set will take obviously O(n) time" comment, this is not actually correct per the doc comments of HashMap:

Iteration over collection views requires time proportional to the "capacity" of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.

So in other words, iterating over the returned Set will take O(n + c) where n is the size of the map and c is its capacity, not O(n). If an inappropriately sized initial capacity or load factor were chosen, the value of c could outweigh the actual size of the map in terms of iteration time.

2 Comments

Duplicate answer. Says what I wrote 9 months ago.
@StephenC I'll have take your word for that, as it looks as though you've deleted that answer and merged it into your other one, and I don't have enough reputation to view deleted answers. I must have missed your answer addressing this point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.