JAVA basics - the difference between ArrayList and LinkedList

Both ArrayList and LinkedList implement the List interface. They have the following differences:

ArrayList is an index-based data interface, and its bottom layer is an array. It can perform random access to elements with O(1) time complexity. Correspondingly, LinkedList stores its data in the form of a doubly linked list. Each element is linked to its previous and next elements. In this case, the time complexity of finding an element is O( n).

Compared with ArrayList, LinkedList's insertion, addition, and deletion operations are faster because when an element is added to any position in the collection, there is no need to recalculate the size or update the index like an array. LinkedList takes up more memory than ArrayList because LinkedList stores two references for each node, one pointing to the previous element and one pointing to the next element.

  1. Because Array is an index-based data structure, it uses indexes to search and read data in the array very quickly. The time complexity of getting data from Array is O(1), but deleting data is very expensive because it requires rearranging all the data in the array.

  1. Compared to ArrayList, LinkedList insertion is faster. Because LinkedList is not like ArrayList, it does not need to change the size of the array, nor does it need to reload all the data into a new array when the array is full. This is the worst case of ArrayList, and the time complexity is O(n), while the time complexity of insertion or deletion in LinkedList is only O(1). ArrayList also needs to update the index when inserting data (in addition to inserting at the end of the array).

  1. Similar to inserting data, LinkedList is also better than ArrayList when deleting data.

  1. LinkedList requires more memory because the position of each index of ArrayList is the actual data, while each node in LinkedList stores the actual data and the position of the previous and next nodes (a LinkedList instance stores two values: Node <E> first and Node<E> last represent the actual node and tail node of the linked list respectively. Each Node instance stores three values: E item, Node next, Node pre).

In what scenarios is it more appropriate to use LinkedList instead of ArrayList?
  1. Applications do not access data randomly. Because if you need the nth element in LinkedList, you need to count sequentially from the first element to the nth data, and then read the data.

  1. Apply more inserting and deleting elements and less reading data. Because inserting and deleting elements does not involve rearranging data, it is faster than ArrayList.

In other words, the implementation of ArrayList uses arrays, and LinkedList is based on linked lists; ArrayList is suitable for searches, and LinkedList is suitable for additions and deletions. If you need asynchronous index-based data access, try to use ArrayList. ArrayList is fast and easy to use. But remember to give a suitable initial size and change the size of the array as little as possible.

Guess you like

Origin blog.csdn.net/DreamEhome/article/details/128812030