user7022858
user7022858

Reputation:

ArrayList, LinkedList and Vector which one is the best for adding or removing the element from the list

I appeared for an interview where interviewer asked me about ArrayList, Linked list and Vector. His question was
ArrayList, LinkedList, and Vector are all implementations of the List interface. Which of them is most efficient for adding and removing elements from the list ? And I was supposed to answer including any other alternatives I may be aware of.
I answered him but he seems little not impressed by my answer.
Can someone tell me more about this ?
Thank you

Upvotes: 2

Views: 6797

Answers (4)

Dhivya
Dhivya

Reputation: 1

LinkedList is implemented using Double linked List,since its perrformance is good at adding/removing elements from/to list. Performance of ArrayList vs. LinkedList : The time complexity comparison is as follows: get() : O(n) add() : O(1) remove() : O(1)

Upvotes: 0

ritesh9984
ritesh9984

Reputation: 418

Choose LinkedList if you have a lot of data to adding and removing from list but be careful if you wish to get element from your list than this will be not right Data Structure.

List<T> list = new LinkedList<T>();

Upvotes: 0

Nemo Vu
Nemo Vu

Reputation: 26

LinkedList is best suited for adding/removing items, reason being you just change the links between the items without manipulating other unrelated items to accomplish current operation. This also makes linked lists comparatively faster than other containers.

Cheers!

Upvotes: 0

vibhor_shri
vibhor_shri

Reputation: 374

  • LinkedList is implemented as a double linked list. It's performance on add and remove is better than Arraylist, but worse on get and set methods.You will have to traverse the list up to a certain point in those cases. So, definitely not LinkedList.

  • ArrayList is implemented as a resizable array. As more elements are added to ArrayList, its size is increased dynamically. It's elements can be accessed directly by using the get and set methods, since ArrayList is essentially an array.

  • Vector is similar with ArrayList, but it is synchronised.

ArrayList is a better choice if your program is thread-safe. Vector and ArrayList require more space as more elements are added. Vector each time doubles its array size, while ArrayList grow 50% of its size each time.

LinkedList, however, also implements Queue interface which adds more methods than ArrayList and Vector, such as offer(), peek(), poll(), etc.

A lot is dependent on what kind of requirement you are working on. A decision can be taken depending upon needs.

Upvotes: 3

Related Questions