When to use ArrayList and LinkedList in JavaArrayList provides constant time for search operation, so it is better to use ArrayList if searching is more frequent operation than add and remove operation. The LinkedList provides constant time for add and remove operations. So it is better to use LinkedList for manipulation. ArrayList has O(1) time complexity to access elements via the get and set methods. LinkedList has O(n/2) time complexity to access the elements. LinkedLinked class implements Deque interface also, so you can get the functionality of double ended queue in LinkedList. The ArrayList class doesn't implement Deque interface. In sort, ArrayList is better to access data wherease LinkedList is better to manipulate data. Both classes implements List interface. ArrayList ExampleOutput: Traversing ArrayList... ankit peter mayank LinkedList ExampleOutput: After adding: [ankit, peter, mayank] After removing: [ankit, mayank] After changing: [ankit, vivek] Next TopicJava Collections Interview Questions |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India