What is the time complexity of the java.util.HashMap 'keySet () method?

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

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

+4
source share
6 answers

Actually, getting a set of keys is O(1)cheap. This is because HashMap.keyset () returns the actual KeySet associated with the HashMap.

Set , HashMap. , , HashMap; clear() HashMap!

+17

, O (1). , , - HashMap.

, O (n), () - O (1), n .

+5

O (n) ... - , () -. , , ( , )...

** EDIT: , keyset() O (1) , , O (n). , **

+2

Java- , , . , , O (1). .

0

" O (n) ", HashMap:

, "" HashMap ( ) ( -). , ( ), .

, Set O(n + c), n - , c - , O(n). , c .

0

:

... Set O(n) .

:

  • HashMap, new HashMap<>(). N . , , N O(N). , O(N).

  • , HashMap new HashMap<>(capacity) ( ) capacity. O(Cap) + O(N) . Cap , O(max(Cap, N)), , O(N).

. capacity int API HashMap, Cap 2 31. Cap N O(N).

, N , 2 38 (256 ) N, Cap. -, . !

0

Source: https://habr.com/ru/post/1724795/


All Articles