Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between arraylist and linkedList [duplicate]

Possible Duplicate:
When to use LinkedList<> over ArrayList<>?
When to use a linked list over an array/array list?

When should I use arrayList and when should I go for LinkedList?

When should I use TreeSet, LinkedHashSet and HashSet?

like image 252
constantlearner Avatar asked Jul 26 '12 11:07

constantlearner


People also ask

Does LinkedList allow duplicates?

A LinkedList can store the data by use of the doubly Linked list. Each element is stored as a node. The LinkedList can have duplicate elements because of each value store as a node.

What is the main difference between ArrayList and LinkedList?

ArrayList internally uses a dynamic array to store its elements. LinkedList uses Doubly Linked List to store its elements. ArrayList is slow as array manipulation is slower. LinkedList is faster being node based as not much bit shifting required.

Is ArrayList more efficient than LinkedList?

Manipulating LinkedList takes less time compared to ArrayList because, in a doubly-linked list, there is no concept of shifting the memory bits. The list is traversed and the reference link is changed. This class implements a List interface. Therefore, this acts as a list.

Which is faster ArrayList or LinkedList?

Manipulation with LinkedList is faster than ArrayList because it uses a doubly linked list, so no bit shifting is required in memory. 3) An ArrayList class can act as a list only because it implements List only. LinkedList class can act as a list and queue both because it implements List and Deque interfaces.


1 Answers

When should i use arrayList and when should I go for LinkedList?

Arraylist maintain indices like arrays. So if want more frequent get operations than put then arraylist is best to go.

LinkedList maintain pointers to elements. you can't to a specific index like in arraylist. But the advantage here in linkedlist is that they don't need to shift back and forth like in arraylist to maintain continues indices. So get operations in linkedlist are costly as you would have to go through pointers to reach your elements. But put operations are good as compared to arraylist. you just need to connect to pointers and that's it.

When should I use TreeSet, LinkedHashSet and HashSet?

the difference is only in ordering. treeset elements need to maintain a specific orders defined by your member objects.

like image 172
Ahmad Avatar answered Sep 29 '22 22:09

Ahmad