1
Sponsored Links


Ad by Google

Most popular question while interview discussion specially in Banking and Telecom domain, because in banking and telecom domain performance is always a matter. We all know that space is buy able but time can't. So let's see what are the differences between ArrayList and LinkedList?
 ArrayList and LinkedList both are the implementation of List data structure so they both have the implementation of optional list methods and also allows null elements.
Performance point of view it depends on the business requirement,
Point #1 : If your business need is fast positional access of element then ArrayList is better which offers constant time positional access vi get(int position) method.

Point #2:; If your business need is adding element frequently into a beginning of the list or traverse the list vi iterator and removing the elements from the list then use LinkedList it offers constant time for these operations.

Point #3 If your business just want to store large number of elements with limited space then must go with ArrayList with initial capacity.

Internal implementation of ArrayList is an Array of object, which is dynamically resizing array and we know that array can be define two ways.

int [] intArray = new int[]{1,2,3,4};
int [] intArray = new int[4]; 
same way we can define ArrayList also when we know the approximate size of the arrayList then we must define with initial capacity,try to run the below code in your machine it will show you the differences between ArrayList and ArrayList with initial capacity.
ArrayList without initial capacity
public static void main(String[] args) {
  List<Long> longList = new ArrayList<Long>();
  long maxValue = 10000000;
  long startTime = System.currentTimeMillis();

  for (long l = 0; l < maxValue; l++) {
   longList.add(l);
  }
  long endTime = System.currentTimeMillis();

  System.out.println("Total execution time required in milli second with out initial capacity=>"+ (endTime - startTime));
 }

Run this with initial capacity and see the differences
ArrayList with initial capacity
public static void main(String[] args) {
  List<Long> longList = new ArrayList<Long>(10000000);
  long maxValue = 10000000;
  long startTime = System.currentTimeMillis();

  for (long l = 0; l < maxValue; l++) {
   longList.add(l);
  }
  long endTime = System.currentTimeMillis();

  System.out.println("Total execution time required in milli second with initial capacity=>"+ (endTime - startTime));
 }
Now run the below code to see the big difference between ArrayList and LinkedList.
LinkedList
public static void main(String[] args) {
  List<Long> longList = new LinkedList<Long>();
  long maxValue = 10000000;
  long startTime = System.currentTimeMillis();

  for (long l = 0; l < maxValue; l++) {
   longList.add(l);
  }
  long endTime = System.currentTimeMillis();

  System.out.println("Total execution time required in milli using LinkedList=>"+ (endTime - startTime));
 }

Yes huge differences between ArrayList and LinkedList while adding element, So think twice before opting one of them about your business requirement.

Few very popular java interview questions:
Which Collection is better to store 10 millions of record between ArrayList and LinkedList.
What are the Key differences between Comparable and Comparator.
Serializable Vs Externalizable.
Java program to find missing numbers
Why String is Immutable in java

Don't forgot to post your comments/feedback/suggestion about this post or if you have something to add on this.
Sponsored Links

1 comments:

  1. you have given fantabulous concept and example here. :)

    ReplyDelete