HashMap vs ArrayList performance am I correct

后端 未结 6 2037
野趣味
野趣味 2020-12-04 09:29

I currently believe that:

  • When you need a structure from which you will be retrieving items randomly - use a HashMap
  • When you will be ret
6条回答
  •  死守一世寂寞
    2020-12-04 09:56

    I'd disagree slightly. For me it depends more on how I want to retrieve the items. If I want to do so based on something like their order (by index, to be precise) I would tend to use a linear structure like an ArrayList (or even an array). If I need to look up items, I'd use a map structure like the HashMap.

    Another complicating factor has to do with insertions and order, as dan pointed out.

提交回复
热议问题