Use ArrayList or LinkedList

0

The List interface has several implementations, including ArrayList and LinkedList . I usually use only ArrayList , but I would like to know in which scenarios it is recommended to use the LinkedList implementation instead ArrayList and if there is any gain in this change.

    
asked by anonymous 05.11.2018 / 17:01

1 answer

4
  

In what scenarios it is recommended to use the LinkedList implementation instead of ArrayList

If you can not justify the use of a LinkedList use ArrayList . It should be the default because it is simple and fast. Linked list can not access any element with constant time complexity (O (1)), must be, on average, linear (O (N / 2)), which can be a little to extremely slow, as the size.

When you need to insert or remove items in the middle of the list, and in some cases even at the end (you can almost always resolve or accept bad cases at the end), then the linked list might be a little more useful. But it is only in some cases, too, because to insert you need to know where it is and this has a cost similar to insertion / removal in a ArrayList , so the gain is lost in most, if not all, operations. p>

Generally when you need a lot of insertion / removal a tree is often better than a list, then both are bad choices. Linked list nowadays is used in quite a few cases, where access actually follows a linked list pattern and not another structure.

  

Is there any gain in this change?

In many trades the gain is brutal.

    
05.11.2018 / 17:50