Write a class that can make comparisons between the efficiency of the common met
ID: 3575777 • Letter: W
Question
Write a class that can make comparisons between the efficiency of the common methods from the List interface in the ArrayList and LinkedList classes such as add, get, and remove. Use the polymorphic-variable technique described above to write the class so that it only knows it is performing its tests on objects of the interface type List rather than on the concrete types ArrayList and LinkedList. Use large lists of objects for the tests, to make the results significant. You can use the currentTimeMillis method of theSystem class for getting hold of the start and finish time of your test methods.
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class ArrayListVsLinkedlIst {
public static void main(String[] args) {
// creating objects of LinkedList and ArrayList
List<Integer> linkedList = new LinkedList<Integer>();
List<Integer> arrayListList = new ArrayList<Integer>();
Random random = new Random();
// adding 10000000 random numbers
for(int i=1; i<=10000000; i++){
int rand = random.nextInt(200);
linkedList.add(rand);
arrayListList.add(rand);
}
// not getting 300000 element
long startTime = System.currentTimeMillis();
int x = arrayListList.get(300000);
long endTime = System.currentTimeMillis();
System.out.println("Time taken by arraylist to get 30000th element: "+(endTime-startTime)+" millis");
startTime = System.currentTimeMillis();
x = linkedList.get(300000);
endTime = System.currentTimeMillis();
System.out.println("Time taken by linked list to get 30000th element: "+(endTime-startTime)+" millis");
startTime = System.currentTimeMillis();
arrayListList.add(30000, 34);
endTime = System.currentTimeMillis();
System.out.println("Time taken by arraylist to add element at 30000th position: "+(endTime-startTime)+" millis");
startTime = System.currentTimeMillis();
linkedList.add(30000, 34);
endTime = System.currentTimeMillis();
System.out.println("Time taken by linked list to add element at 30000th position: "+(endTime-startTime)+" millis");
startTime = System.currentTimeMillis();
arrayListList.remove(30000);
endTime = System.currentTimeMillis();
System.out.println("Time taken by arraylist to remove an element at 30000th position: "+(endTime-startTime)+" millis");
startTime = System.currentTimeMillis();
linkedList.remove(30000);
endTime = System.currentTimeMillis();
System.out.println("Time taken by linked list to remove element at 30000th position: "+(endTime-startTime)+" millis");
}
}
/*
Sample run:
Time taken by arraylist to get 30000th element: 0 millis
Time taken by linked list to get 30000th element: 4 millis
Time taken by arraylist to add element at 30000th position: 4 millis
Time taken by linked list to add element at 30000th position: 0 millis
Time taken by arraylist to remove an element at 30000th position: 4 millis
Time taken by linked list to remove element at 30000th position: 1 millis
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.