• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between LinkedList and ArrayList  RSS feed

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its stated that LinkedList class provides methods get, insert and remove an element at the beginning and end of the list. But same things can be achieved by using ArrayyList add() and get(). These methods provide options to add elements at anywhere in list and by default added at tail or removed from head. Both are not synchronized So whats the advantage of using LinkedList ?

Thanks
 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

Tha advantage of LinkedList is in its linked list structure. It is infinite and when we need to add and remove mappings sequentially it is BEST to use LinkedList.
Because no backing array recreation is done. We do not care about backing array and its size as LinkedList builds collection by references. One object references the other and so on in both directions.

Please cover my Internal life of LinkedList tutorial and Internal life of ArrayList tutorial
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nirjari patel wrote:These methods provide options to add elements at anywhere in list and by default added at tail or removed from head. Both are not synchronized So whats the advantage of using LinkedList ?

Volodymyr is quite right. The fact that ArrayLists are based on arrays is a blessing and a curse (mostly the first): Fabulously fast for retrieval; lousy for any kind of adding or removal, except at the end. LinkedLists: lousy for retrieval (except at the start or end); fabulously fast for additions and removals at the start or end or when you have a position.

For most "normal" applications, fast random retrieval is by the far the most important thing, which is why you'll see ArrayLists used a lot more than LinkedLists; but just try using one like a Stack, and you'll soon see how slow they can be.

Winston
 
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One interesting thing is that LinkedList takes more memory than ArrayList of the same size, and this is true even if you don't specify correct capacity for ArrayList upfront. ArrayList needs one reference per item in collection (when sized correctly), while LinkedList needs three references - it is a double linked list.

Nowadays I'd use LinkedList only after positively identifying performance problem with ArrayList with a profiler (or as a Queue/Deque implementation, but I consider this different from a plain old list, even though LinkedList implements it too). Nearly all of the time, I add to the end of the list anyway.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!