• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList or LinkedList

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which is faster for Iteration ? An ArrayList or a LinkedList ? What about insertions and deletions ?
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chetan Raju:
Which is faster for Iteration ? An ArrayList or a LinkedList ? What about insertions and deletions ?



Well about your question

LinkedList is a doubly linked list allowing constant time access to the first and last elements, constant time appending of a single element to the end of the list or prepending a single element to the beginning of the list (deletion is also quick from the front or back, and deletion near the front or back has low overhead), linear time random access to elements in the middle, and linear time iteration over all elements in order.

ArrayLists allow constant time random access to all elements, amortized constant time appending of a single element to the end of a list but gets sluggish if you want to add and delete stuff from places other than the end of the list, particularly the beginning.

LinkedLists will have a higher memory overhead since even in the most efficient memory representation each element maintains two extra pointers, whereas an ArrayList maintains an array no more than twice as large as contents which won't cost more memory than maintaining a single extra pointer per element and will usually cost less. LinkedLists will also have an additional garbage collection overhead since a object is created for each element.

LinkedLists make handy queues and are also good when there won't be random access but there will be iteration with an Iterator or ListIterator along with calls to remove(). When you don't need to add or delete items in the middle or front of the list an ArrayList is more appropriate.

Well Hope you have cleared your doubts now....

Well about the more or less comparasions check out following links

Final Performance Testing - Examples
 
Shaan Shar
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to add and remove the elements frequently then use LinkedList , for Simple retrieve use ArrayList because for simple retrieval ArrayList is faster than LinkedList

The concept used for the implementation is a wholly different one. An ArrayList is, as the name states, backed by an array (so is a Vector); access occurs index-based.
The LinkedList is a "chain" of wrappers around the contained objects (these wrappers are "linked" to their neighbour(s), only they know who it is), and the only "fixed" thing, i.e. information kept by the LinkedList's instance, is the "head" of the List (and eventually a "tail" too, for reverse access). Access occurs via iteration along the chain (therefore the faster sequential access, object removal and addition, and the slower random access).
As far as i've experienced it, the LinkedList's concept is covered in one of the first chapters of every object-based language's tutorial book.

Hope it helps you out.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ankur Sharma:
If you want to add and remove the elements frequently then use LinkedList



Unless you only need to add and remove at the end of the list.

for Simple retrieve use ArrayList because for simple retrieval ArrayList is faster than LinkedList



Unless the only form of "retrieve" you need is iteration over all elements.
 
reply
    Bookmark Topic Watch Topic
  • New Topic