-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmethods of List, Set and Map Interfaces
More file actions
24 lines (22 loc) · 1.78 KB
/
methods of List, Set and Map Interfaces
File metadata and controls
24 lines (22 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Source: http://www.acnenomor.com/2331736p1/time-complexity-of-hashmap-methods
remove: O(1) [method of List, Set, Map classes]
size: O(1) [method of List, Set, Map classes]
values: O(n) (on traversal through iterator) [method of Map class]
contains: O(1) [method of HashSet] {uses hash function}
contains: O(n) [method of ArrayList] {to understand this, please understand first how ArrayList works internally.
ArrayList does not contain any hash method. Its actually an array as its name suggests}
How does ArrayList work internally ?
Ans: http://stackoverflow.com/questions/3467965/how-does-arraylist-work
containsKey: O(1) [method of HashMap]
{Source: http://stackoverflow.com/questions/8923251/what-is-the-time-complexity-of-hashmap-containskey-in-java}
containsValue: O(n) [method of HashMap] {since hash function only exists for key and not value,
hence it has to iterate through all elements of the HashMap to find the value. Hence O(n) operation
Source: http://stackoverflow.com/questions/16757359/what-is-the-time-complexity-of-hashmap-containsvalue-in-java}
VERY VERY HELPFUL TABLE OF TIME COMPLEXITY ANALYSIS:
http://www.c-sharpcorner.com/UploadFile/0f68f2/comparative-analysis-of-list-hashset-and-sortedset/
I think that remove() will be the same complexity as get(), O(1), assuming we don't have a giant HashMap with equal
hashCodes, etc etc...
For size() i'd also assume O(1), since a HashSet, which also has no order, has a size() method with complexity O(1).
The one i have no idea of is values() - I'm not sure whether this method will just somehow "copy" the HashMap,
giving a time complexity of O(1), or if it will have to iterate over the HashMap, making the complexity equal
to the amount of elements stored in the HashMap.