import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class CollectionPerformance {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // testGeneric();
        testCollection();
    }

    public static long testGet(List a, int repetition) {
        long startTime = System.currentTimeMillis();
        for (int j = 0; j != repetition; j++) {
            for (int i = 0; i != a.size(); i++) {
                String t = (String) a.get(i);
            }
        }
        long endTime = System.currentTimeMillis();
        return endTime - startTime;
    }

    public static long testIterator(List a, int repetition) {
        long startTime = System.currentTimeMillis();
        for (int j = 0; j != repetition; j++) {
            for (Iterator it = a.iterator(); it.hasNext();) {
                String t = (String) it.next();
            }
        }
        long endTime = System.currentTimeMillis();
        return endTime - startTime;
    }

    public static void testCollection() {
        int repetition = 100;
        // List a = new ArrayList();
        List a2 = new LinkedList();
        for (int i = 0; i != 30000; i++) {
            a2.add("a");
        }
        testGet(a2,1);
        for (int count = 10000; count <= 100000; count = count + 10000) {
            List a = new LinkedList();
            for (int i = 0; i != count; i++) {
                a.add("a");
            }
            System.out.print("\tCount:" + count);            
            System.out.print("\tIterator:" + testIterator(a, 1));
            System.out.println("\tGet:" + testGet(a, 1));
        }
    }

    public static void testGeneric() {
        int repetition = 100;
        List<String> a = new ArrayList<String>();
        for (int i = 0; i != 1000000; i++) {
            a.add("abc");
        }
        long startTime, endTime;

        startTime = System.currentTimeMillis();
        for (int j = 0; j != repetition; j++) {
            for (String t : a) {
                String b = t;
            }
        }
        endTime = System.currentTimeMillis();
        System.out.println("Enhanced for:" + (endTime - startTime));

        startTime = System.currentTimeMillis();
        for (int j = 0; j != repetition; j++) {
            StringBuffer sb = new StringBuffer();
            for (Iterator<String> it = a.iterator(); it.hasNext();) {
                String t = it.next();
                if (t.indexOf("b") != -1) {
                    sb.append(t);
                }
            }
        }
        endTime = System.currentTimeMillis();
        System.out.println("Iterator:" + (endTime - startTime));

        startTime = System.currentTimeMillis();
        for (int j = 0; j != repetition; j++) {
            int size = a.size();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i != a.size(); i++) {
                String t = a.get(i);
                if (t.indexOf("b") != -1) {
                    sb.append(t);
                }
            }
        }
        endTime = System.currentTimeMillis();
        System.out.println("int i:" + (endTime - startTime));

    }
}
