Use Arraylist or Linkedlist

Asked

Viewed 325 times

0

The interface List has several implementations, among them ArrayList and LinkedList. Usually I have been using only ArrayList, but would like to know in which scenarios it is recommended to use the implementation LinkedList instead ArrayList and if there’s any gain in that change.

  • 2

    Take a look at this link: https://www.devmedia.com.br/differca-entre-arraylist-vector-e-linkedlist-em-java/29162

1 answer

4


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

If you cannot justify using a LinkedList use ArrayList. It must be the standard for being simple and fast. Linked list cannot access any element with constant time complexity (O(1)), it needs to be, on average, linear medium (O(n/2)), which can be a little slower, depending on 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 solve or accept bad cases at the end), then the linked list can be a little more useful. But it’s only in some cases too, because to insert you need to know where it is and this has a cost similar to inserting/removing in a ArrayList, then the gain is lost in much of the transactions, if not in all.

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

There’s some gain in that change?

In several operations the gain is brutal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.