ListDemoHead.java Source code

Java tutorial

Introduction

Here is the source code for ListDemoHead.java

Source

/*
time for LinkedList = 62
time for ArrayList = 7488
*/
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ListDemoHead {
    static final int SIZE = 100000;

    static long timeList(List list) {
        long start = System.currentTimeMillis();
        Object obj = new Object();
        for (int i = 0; i < SIZE; i++) {
            // add object to the head of the list
            list.add(0, obj);
        }

        return System.currentTimeMillis() - start;
    }

    public static void main(String args[]) {
        // do timing for LinkedList
        System.out.println("time for LinkedList = " + timeList(new LinkedList()));

        // do timing for ArrayList
        System.out.println("time for ArrayList = " + timeList(new ArrayList()));
    }
}