Difference between ArrayList and LinkedList in Java
ArrayList class and LinkedList class are classes of Java with following syntax:
ArrayList class Syntax:
ArrayList<String> al=new ArrayList<String>();//creating new generic arraylist
LinkedList class Syntax:
LinkedList<String> ll=new LinkedList<String>();
Implementation: ArrayList can be defined as array implementation of list interface which is resizable , while LinkedList is defined as Doubly-linked list implementation of the list interface. Both extends AbstractList class and implements List interface.
Performance: The type of operation ArrayList and LinkedList perform, determines their performance.
get(int index) or search operation : ArrayList internally used array data structure therefore have index based system and thereby search operation run in O(1) time but in case of LinkedList index based system is not used elements are iterated either from beginning or end thereby search operation in O(n) time.
insert() or add(Object) operation: This operation is faster in LinkedList than in ArrayList because adding element at particular index require shifting of elements that is not the case with LinkedList.
Usage: ArrayList is better for storing and accessing data while LinkedList is better formanipulating data.
Initial Capacity : ArrayList creates an empty list of initial capacity 10 , while LinkedList only constructs the empty list without any initial capacity only if constructor is not overloaded.
Memory Overhead : Node in LinkedList needs to maintain the addresses of next and previous node while in ArrayList actual object(data) is stored in indexes therefore memory overhead in LinkedList is more as compared to ArrayList.
0 Comment(s)