9
Difference between ArrayList and LinkedList

Difference between ArrayList and LinkedList in Java

Embed Size (px)

Citation preview

Page 1: Difference between ArrayList and LinkedList in Java

Difference between ArrayList and

LinkedList

Page 2: Difference between ArrayList and LinkedList in Java

1. Implementation difference.• ArrayList<E> as we know is backed by an array with initial capacity of

10. If that array is filled then contents are moved to new array who’s size is roughly 1.5 times of previous array.

• LinkedList<E> is an implementation of Linked List data structure. Inner private static class Node in LinkedList class defines the structure of node using the constructor taking 3 parameters as previous pointer, element and next pointer.

Page 3: Difference between ArrayList and LinkedList in Java

2. Performance (depends on operation that we perform)• add(E e)

• ArrayList<E> adds element at the end of the list. This can be done in O(1) time. But let’s say that size of backing array is to be incremented then old elements is to be copied to new array and then new element is appended. This takes O(n) time.

• In LinkedList<E> this operation takes O(1) time. The reason is LinkedList<E>

maintains the tail pointer and hence element is just appended by setting the links.

Page 4: Difference between ArrayList and LinkedList in Java

2. Performance continued.• remove(int index) and remove(Object o)

• ArrayList<E> removes element once it is able to find it. If element is found then all other elements in backing array are shifted one place. This requires O(n) time.

• LinkedList<E> has several implementations of remove such as• removeFirst() – removes first element. O(1) time.• removeLast()– removes last element. O(1) time.• remove(int index) – removes element at index. O(n) time.• remove(Object o) – removes element by searching it. O(n) time.• remove() – removes first element. O(1) time. This method calls removeFirst.• removeFirstOccurrence(Object o) – removes first occurrence of Object.• removeLastOccurrence(Object o) – removes last occurrence of Object.

Page 5: Difference between ArrayList and LinkedList in Java

2. Performance continued.• get(int index) • ArrayList<E> gets the element at index in O(1).

• LinkedList has 3 different method that returns the element.

• get(int index) – returns the element at index O(n) time because link list does not provide the indexed retrieval.

• getFirst() – returns the first element from the list.• getLast() – returns the last element from the list.

Page 6: Difference between ArrayList and LinkedList in Java

3. Iteration.• ArrayList<E> provides 2 iterators. Iterator<E> and ListIterator<E>.

Study about their differences here. Traverse or iterate ArrayList in 6 different ways.

• LinkedList<E> provides 3 iterators. Iterator<E>, ListIterator<E> and

descendingIterator(). descendingIterator() it has 3 method of Iterator that works in descending manner i.e. from back to front.

Page 7: Difference between ArrayList and LinkedList in Java

4. Capacity.• ArrayList<E> has initial capacity of 10 elements. • LinkedList<E> does not define any capacity. It just creates new nodes

and appends them.

Page 8: Difference between ArrayList and LinkedList in Java

5. Memory Overhead.• In ArrayList<E> backing array is used to store element at index so

memory overhead is 0 (zero). • In LinkedList<E> previous and next links are to be maintained which

can be troublesome. So it has memory overhead.

Page 9: Difference between ArrayList and LinkedList in Java

That’s all folks.