20-03-25 ---------- Q)What is an ArrayList? =>java.util.ArrayList is a list based collection class. =>ArrayList is the most widely used list based collection class. =>It uses dynamic array(both growable and shrinkable array) as underlying data structure. =>It is non-synchronized. =>ArrayList is best suited for rea-only lists. =>ArrayList compromises performance while manipulating its elements as the elements are to be shifted. DIAGRAM Q)What are the important methods of ArrayList? =>that of java.util.Collection & java.util.List interfaces Note:- Collection interface and List interface exampe programs are of ArrayList only. Q)What is LinkedList? =>LinkedList is a list based collection class. =>Insertion order is maintained. =>It can contain duplicate elements. =>It is non-synchronized. =>In the background it uses doubly linked list data structure. =>Insertions and removals are very effcient as no shifting of elements is involved. DIAGRAM Q)Java program to compare the performance between ArrayList and LinkedList for rea-only operation. import java.util.*; class PerformanceTest { public static void main(String[] args) { LinkedList llist=new LinkedList<>(); ArrayList alist=new ArrayList<>(); Collections.addAll(llist,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8); Collections.addAll(alist,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8); Iterator iterator1=llist.iterator(); long t1=System.nanoTime(); while(iterator1.hasNext()) iterator1.next(); long t2=System.nanoTime(); long timetaken=t2-t1; System.out.println("Time taken to read the linked list :"+timetaken+" nano seconds"); Iterator iterator2=alist.iterator(); t1=System.nanoTime(); while(iterator2.hasNext()) iterator2.next(); t2=System.nanoTime(); timetaken=t2-t1; System.out.println("Time taken to read the array list :"+timetaken+" nano seconds"); } } Q)What are the differences between ArrayList and LinkedList? ArrayList LinkedList ****************************************************** 1)underlying data structure doubly-linked list is resizable array. 2)elements are stored no in contiguous memory locations. 3)It is best suited best suited for manipulated lists for read-only lists 4)It is an array like It acts as a list,stack collection object and queue. with API support to deal with the elements. ******************************************************