Javatpoint Logo

which is the fastest interface to insert element in middle

By: priyan*** On: Wed Jul 12 09:51:34 IST 2017     Question Reputation0 Answer Reputation0 Quiz Belt Series Points0  0Blank User
Which implementation of the List interface provides for fastest insertions of a new element into the middle of the list?Up0Down

 
Insertion and deletion of element are fast in linkedlist as compared to arraylist. See below example.

ArrayList<Integer> arrayList = new ArrayList<Integer>();
LinkedList<Integer> linkedList = new LinkedList<Integer>();
long startTime=0, endTime=0, duration=0 ;

startTime = System.nanoTime();
for (int i = 0; i < 100000; i++) {
arrayList.add(i,i);
}
endTime = System.nanoTime();
duration = endTime - startTime;
System.out.println("arrayList add: " + duration);

startTime = System.nanoTime();
for (int i = 0; i < 100000; i++) {
linkedList.add(i, i);
}
endTime = System.nanoTime();
duration = endTime - startTime;
System.out.println("LinkedList add: " + duration);
Image Created0Down

By: [email protected] On: Sun Jul 16 12:28:14 IST 2017 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No