// Name: B6-24 // Date: 12/6/19 import java.util.*; public class CollectionsSpeed { public static final int n = 10000; public static void main(String[] args) { ArrayList alist = new ArrayList(); LinkedList llist = new LinkedList(); makeValues(alist, llist); System.out.println("get each for ArrayList = " + timeGetEach(alist)); System.out.println("get each for LinkedList = " + timeGetEach(llist)); System.out.println("\nadd at 0 for ArrayList = " + timeAddFirst(alist)); System.out.println("add at 0 for LinkedList = " + timeAddFirst(llist)); System.out.println("\nadd at list.size() for ArrayList = " + timeAddLast(alist)); System.out.println("add at list.size() for LinkedList = " + timeAddLast(llist)); System.out.println("addLast for LinkedList = " + timeAddLastLL(llist)); } public static void makeValues(ArrayList alist, LinkedList llist) { for( int i = 0; i < n; i++ ) { alist.add(i); llist.add(i); } } /** * Get n items by searching for each one. */ public static double timeGetEach(List list) { double start = System.nanoTime(); for( int i = 0; i < n; i++ ) { int index = list.get(i); } return (System.nanoTime() - start)/1E6; } /** * Add 10000 new objects at position 0. */ public static double timeAddFirst(List list) { double start = System.nanoTime(); for( int i = 0; i < n; i++ ) { list.add(0, i); } return (System.nanoTime() - start)/1E6; } /* * Add 10000 new objects at position list.size() */ public static double timeAddLast(List list) { double start = System.nanoTime(); for( int i = 0; i < n; i++ ) { list.add(list.size()); } return (System.nanoTime() - start)/1E6; } /* * Add 10000 new objects at the end. * Uses the LinkedList method addLast(). * You must cast List list into a LinkedList. */ public static double timeAddLastLL(List list) { double start = System.nanoTime(); LinkedList llist = (LinkedList) list; for( int i = 0; i < n; i++ ) { llist.addLast(i); } return (System.nanoTime() - start)/1E6; } }