For a HashMap<String, String> map
each time a key-value pair is inserted into the map a hash is calculated -
java.lang.String#hashCode
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
As it's pretty self-explanatory, the complexity of put operation is basically the complexity of the hash calculation.
So what should be the appropriate way to define hashmap worst-case time complexity for put/get operation?
If you have the same question from hash collision perspective, here you can find the answer: Is a Java hashmap really O(1)?