Java - Difference between LinkedList and ArrayList

http://javarevisited.blogspot.sg/2012/02/difference-between-linkedlist-vs.html

Because sometimes I can't open this page without proper proxy configuration, so I have to copy some content here.


Difference between LinkedList and ArrayList

LinkedList and ArrayList both implement List Interface but how they work internally is where the differences lies. Main difference between ArrayList and LinkedList is that ArrayList is implemented using re sizable array while LinkedList is implemented using doubly LinkedList.

1) Since Array is an index based data-structure, searching or getting element from Array with index is pretty fast. Array provides O(1) performance for get(index) method, but remove is costly in ArrayList as you need to rearrange all elements. On the Other hand LinkedList doesn't provide Random or index based access and you need to iterate over linked list to retrieve any element which is of order O(n).

2) Insertions are easy and fast in LinkedList compared to ArrayList, because there is no risk of resizing array
and copying content to new array. If array gets ful, Insertion makes adding into ArrayList of O(n) in worst case, while adding is O(1) operation in LinkedList in Java. ArrayList also needs to update its index if you insert something anywhere except at the end of array.

3) Like insertions, removal is  better in LinkedList than ArrayList.

4) LinkedList has more memory overhead than ArrayList, because in ArrayList each index only holds actual object (data) but in LinkedList each node holds both data and address of next and previous node.

When to use LinkedList and ArrayList in Java

ArrayList is more popular among Java programmer than LinkedList as there are few scenarios on which LinkedList is a suitable collection than ArrayList. but still there are situation where a LinkedList is better choice than ArrayList in Java. Use LinkedList in Java if:

1) Your application can live without Random access. Because if you need nth element in LinkedList you need to first traverse up to nth element O(n) and than you get data from that node.

2) Your application is more insertion and deletion than retrieval. Since insertion or removal doesn't involve resizing, it's much faster than ArrayList.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值