ArrayList vs LinkedList from memory allocation perspective

后端 未结 5 1802
时光取名叫无心
时光取名叫无心 2020-12-01 00:58

I need to store a large amount of information, say for example \'names\' in a java List. The number of items can change (or in short I cannot predefine the size). I am of th

5条回答
  •  自闭症患者
    2020-12-01 01:23

    ArrayList use one reference per object (or two when its double the size it needs to be) This is typically 4 bytes.

    LinkedList uses only the nodes its needs, but these can be 24 bytes each.

    So even at it worst ArrayList will be 3x smaller than LinkedList.

    For fetching ARrayList support random access O(1) but LinkedList is O(n). For deleting from the end, both are O(1), for deleting from somewhere in the middle ArrayList is O(n)

    Unless you have millions of entries, the size of the collection is unlikely to matter. What will matter first is the size of entries which is the same regardless of the collection used.

提交回复
热议问题