Hashmap vs Array performance

前端 未结 7 1206
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 18:29

Is it (performance-wise) better to use Arrays or HashMaps when the indexes of the Array are known? Keep in mind that the \'objects array/map\' in the example is just an exam

相关标签:
7条回答
  • 2020-12-02 19:33

    Logically, HashMap is definitely a fit in your case. From performance standpoint is also wins since in case of arrays you will need to do number of string comparisons (in your algorithm) while in HashMap you just use a hash code if load factor is not too high. Both array and HashMap will need to be resized if you add many elements, but in case of HashMap you will need to also redistribute elements. In this use case HashMap loses.

    0 讨论(0)
提交回复
热议问题