LinkedList: Java Collections Framework Video Tutorial, Part 2

The second part of the tutorial series on the Java Collections Framework; in this tutorial I explain when to use LinkedList rather than ArrayList; we look at how the two classes work internally and I also explain a bit about the List interface.

After starting the video, click the maximise button to make it fullscreen so you can see the code!

Code For This Tutorial

App.java:

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


public class App {


	public static void main(String[] args) {
		/*
		 * ArrayLists manage arrays internally.
		 * [0][1][2][3][4][5] ....
		 */
		List<Integer> arrayList = new ArrayList<Integer>();
		
		/*
		 * LinkedLists consists of elements where each element
		 * has a reference to the previous and next element
		 * [0]->[1]->[2] ....
		 *    <-   <-
		 */
		List<Integer> linkedList = new LinkedList<Integer>();
		
		doTimings("ArrayList", arrayList);
		doTimings("LinkedList" , linkedList);
	}
	
	private static void doTimings(String type, List<Integer> list) {
		
		for(int i=0; i<1E5; i++) {
			list.add(i);
		}
		
		long start = System.currentTimeMillis();
		
		/*
		// Add items at end of list
		for(int i=0; i<1E5; i++) {
			list.add(i);
		}
		*/
		
		// Add items elsewhere in list
		for(int i=0; i<1E5; i++) {
			list.add(0, i);
		}
		
		long end = System.currentTimeMillis();
		
		System.out.println("Time taken: " + (end - start) + " ms for " + type);
	}
	


}

Time taken: 7546 ms for ArrayList
Time taken: 76 ms for LinkedList

Learn to program Java Swing with my complete video course – desktop programming and applets. Includes 7 free videos. Click here for details.

All pages on this site are copyright © 2013 John W. Purcell

5 Responses to LinkedList: Java Collections Framework Video Tutorial, Part 2

  1. Purvesh says:

    Hello John,

    All your tutorials are fantastic.
    You give a very detailed explanation and try to give as much info as u can. Also you dont waste time during your lectures like other people do. Thanks for saving our bandwidth !! :)
    It would be wonderful, if u could add tutorials on Struts2, Spring, Hibernate, MySQL.

    Waiting for them….!!!

  2. Charles says:

    Hello John,
    I am Charles,You’re tutorial are excellent and to the point and wonderful explanation and many thanks.

    Charles

  3. Tapan says:

    Hello John,

    You tutorials on Collections are awesome. Thanks a lot. Would appreciate if you can put tutorials on Hibernate and Spring as well.

    Regards,
    Tapan

  4. agape says:

    amazing!!

  5. You tutorial is simple comprehensible Thanks a lot
    with due Respect
    Antonio

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Posted in Java, Java Collections Framework | Tagged , , , , |