Domanda di colloquio di youngculture

What is faster for reading in Java ArrayList or HashMap and why?

Risposte di colloquio

Anonimo

28 giu 2016

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

Anonimo

28 giu 2016

To conclude my previous post: if we are not talking about best case scenario for the HashMap it is safe to say that ArrayList is faster for reading.