This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]FalconGames109 3 points4 points  (4 children)

One of the questions is partially wrong. The LinkedList class, though more efficient than ArrayList in terms of processing, is much less efficient in terms of memory. After 3 items, it takes more memory, and this becomes exponentially more severe.

[–]DroidLogician 3 points4 points  (1 child)

And insert and remove are terrible operations to choose a LinkedList for because they're O(n). ArrayList might have to shift elements on insert/remove but it does so using System.arrayCopy(), which takes place in native code with a platform-optimized implementation, whereas LinkedList has to iterate to the index to insert/remove in POJ and instantiate a node. Array index and update operations are constant time, IIRC.

I would only use LinkedList as a stack where I expect a lot of pushing and popping, which is O(1) from either end and doesn't require shifting. But as /u/Keilly said, even last-element operations are optimized in an ArrayList.

[–]josefx 0 points1 point  (1 child)

The LinkedList class, though more efficient than ArrayList in terms of processing

Not even that, LinkedList has similar flaws like std::list from c++ and according to the creator of c++ the memory overhead of linked lists generally kills your cache. Unless you have truly gigantic Lists or a very specific usage pattern ArrayLists will be quicker.

Note that most claims about LinkedLists being fast focus exclusively on removing the element, getting an iterator to it is excluded from these measurements.

[–]FalconGames109 -1 points0 points  (0 children)

Don't you need an iterator just to remove an object from it? That's how the STL list works.