How do I select the right List implementation?

cf stands with Monica

From this CodeReview answer,

You seem to use ArrayList for all purposes. There are other List-types in Java that suit certain situations better than an ArrayList. You should have a look at those and try to get a feeling when to use which list. In this particular case i.E. a LinkedList is better.

I also tend to use ArrayList fairly heavily, and don't see the logic behind selecting another list type.

The List docs show five main List subclasses: ArrayList, CopyOnWriteArrayList, LinkedList, Stack, and Vector.

From the ArrayList docs,

The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation.

This suggests that ArrayList will often outperform LinkedList (an assertion supported by this heavily upvoted question), although the LinkedList docs don't give a good idea of performance:

All of the operations perform as could be expected for a doubly-linked list.

CopyOnWriteArrayList only seems useful for unchanging lists, since the full snapshot on every modification seems ridiculously expensive for normal use.

Even the Stack docs don't recommend using it:

A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class.

Since Vector is synchronized and the rest of the List subclasses are not, it seems to me that Vector would be the best choice in a threadsafe environment.

Even after reading through the docs, however, I still don't think I understand where TwoThe's answer came from. CopyOnWriteArrayList and Vector each seem to have one specialized use case, Stack doesn't seem worth using, and ArrayList seems superior to LinkedList.

What am I missing here, and under what circumstances would another List implementation be superior to ArrayList?

Gene

I agree that ArrayList is the right choice for many uses. LinkedList uses 8 or 16 bytes of memory per element for pointers, and indexing is O(length) as you've said.

What are LinkedLists advantages, then?

  • Deletion with remove() during iteration is constant time. With ArrayList it's O(length).
  • Adding a new list node always requires the same amount of time. When ArrayLists run out of space, a bigger block of memory is allocated behind the scenes, and all contents are copied. While the amortized time for this over many operations is constant per element, the cost of an individual add() is O(length). If this kind of periodic delay is not acceptable, you can't use ArrayList.

As to the others, Vector goes back to the earliest days of Java. It's thread safe. Because this adds cost to each operation, its use is more-or-less deprecated in favor of ArrayList. When you need thread safety, you can use the SynchronizedList wrapper around an ArrayList. Similarly Stack is more-or-less deprecated in favor of the more modern and not threadsafe Deque.

CopyOnWriteArrayList is a thread safe data list that gets its safety through the somewhat unusual measure of making a copy of the complete array any time the any element changes. While this sounds crazy, it makes sense if there are many threads iterating over the same array because the change doesn't have to wait for the iterations all to complete, which with other concurrent lists is normally the case.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I use this Linked List implementation?

From Dev

How do I select a link with a right arrow as the text with xpath?

From Dev

How do I select a link with a right arrow as the text with xpath?

From Dev

How do I select everything in a list after the first occurence of?

From Dev

How do I save a select option list from foreach loop

From Dev

How do I apply OrderBy to a Linq Query for a select list

From Dev

How do I create a SELECT statement with a list as a parameter?

From Dev

How do I prepopulate the value of Select List in Bootstrap?

From Dev

Maya, PYTHON: how do i select all but one in a list?

From Dev

How do I randomly select a variable from a list, and then modify it in python?

From Dev

How do I select a name from a list on python 2.7

From Dev

How do I create a SELECT statement with a list as a parameter?

From Dev

How do I save a select option list from foreach loop

From Dev

How do I add images in this select list? Javascript?

From Dev

How do I select a set number of variables out of a list on Python

From Dev

How do I write a nested list of lists with my custom list implementation?

From Dev

How do I make xcodebuild select right target when their products have the same name?

From Dev

How do I uncheck the other selected list items that was previously select after I click on "Select All"?

From Dev

How is this implementation called and is it "right"?

From Dev

How do I move a list's bullet point to the right side of the text?

From Dev

How do I make my link list appear in order from top to bottom, not right to left?

From Dev

Using LINQ I have a list of lists, how do I select all objects that exist in every list?

From Dev

I want to make a regular select list with AngularJs have a preselected option, how do I do that?

From Dev

How do i move an object to the right using the arrow key right?

From Java

How do I right align div elements?

From Dev

How do I float a <li> to the right in CSS?

From Dev

How do I make this right content responsive?

From Dev

How do I subquery the right results

From Dev

How do I position the icon to the right of the heading?

Related Related

  1. 1

    How do I use this Linked List implementation?

  2. 2

    How do I select a link with a right arrow as the text with xpath?

  3. 3

    How do I select a link with a right arrow as the text with xpath?

  4. 4

    How do I select everything in a list after the first occurence of?

  5. 5

    How do I save a select option list from foreach loop

  6. 6

    How do I apply OrderBy to a Linq Query for a select list

  7. 7

    How do I create a SELECT statement with a list as a parameter?

  8. 8

    How do I prepopulate the value of Select List in Bootstrap?

  9. 9

    Maya, PYTHON: how do i select all but one in a list?

  10. 10

    How do I randomly select a variable from a list, and then modify it in python?

  11. 11

    How do I select a name from a list on python 2.7

  12. 12

    How do I create a SELECT statement with a list as a parameter?

  13. 13

    How do I save a select option list from foreach loop

  14. 14

    How do I add images in this select list? Javascript?

  15. 15

    How do I select a set number of variables out of a list on Python

  16. 16

    How do I write a nested list of lists with my custom list implementation?

  17. 17

    How do I make xcodebuild select right target when their products have the same name?

  18. 18

    How do I uncheck the other selected list items that was previously select after I click on "Select All"?

  19. 19

    How is this implementation called and is it "right"?

  20. 20

    How do I move a list's bullet point to the right side of the text?

  21. 21

    How do I make my link list appear in order from top to bottom, not right to left?

  22. 22

    Using LINQ I have a list of lists, how do I select all objects that exist in every list?

  23. 23

    I want to make a regular select list with AngularJs have a preselected option, how do I do that?

  24. 24

    How do i move an object to the right using the arrow key right?

  25. 25

    How do I right align div elements?

  26. 26

    How do I float a <li> to the right in CSS?

  27. 27

    How do I make this right content responsive?

  28. 28

    How do I subquery the right results

  29. 29

    How do I position the icon to the right of the heading?

HotTag

Archive