What is faster for reading in Java ArrayList or HashMap and why?
Anonimo
In ArrayList, elements are accessed by integer index, so o(1) is algorithm complexity in any scenario. In case of best case scenario: HashMap with no collisions, it indeed has o(1) algorithm complexity, BUT: In HashMap, elements are accessed by key which includes hashing. Now in worst-case scenario with n collisions on the keys Key1… KeyN with same “keys hash values” we are trying to retrieve element for, hashmap has o(n) lookup time(since it first finds the bucket which contains a Linked list of n elements in wich we are looking for the KeyT, 1