Java Collection Method Framework

Java Collection Method Framework


What im going to attempt to explain today is some collections in Java, ill start ArrayList and compare it to Linklist methods.

If you need to add items to the beginning or the middle of a list Linklist will be faster, if its at the end use ArrayList.

ArraysList manage arrays internally. [0][1][2][3][4].......

LinkLists elements have reference to previous and next elements.. [0]<->[1]<->[2]<->[3]

---------------------------------------------------------------------------------------------------

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

class testList
{
public static void main(String args[])
{
ArrayList <Integer> Arraylist1 = new ArrayList<Integer>();
LinkedList <Integer> LinkedList1 = new LinkedList<Integer>();
 
 
DoTimings("Array List", Arraylist1);
DoTimings("Linked List",LinkedList1);
 
}

private static void DoTimings(String type, List<Integer> x)
{
for(int z=0; z<1E5; z++) // 100,000 items
x.add(z);
long start = System.currentTimeMillis();
for(int a=0; a<1E5; a++) // 100,000 items
x.add(a);
long end = System.currentTimeMillis();
System.out.println("Time taken : "+(end-start)+" ms for "+type);
}
}



-------------------------------------OUTPUT----------------------------------------------
Time taken : 16 ms for Array List
Time taken : 15 ms for Linked List


No comments:

Post a Comment