1 package com.test; 2 3 import java.util.List; 4 import java.util.ArrayList; 5 6 class MyCollection{ 7 8 public static void populateList(List<Long> l, int multiple){ 9 for(int i=0; i<50000; i++){ 10 Long value = Long.valueOf(i*multiple); 11 l.add(value); 12 } 13 } 14 15 public static void main(String[] args) { 16 List<Long> listOf2 = new ArrayList<Long>(); 17 populateList(listOf2, 2); 18 19 List<Long> listOf3 = new ArrayList<Long>(); 20 populateList(listOf3, 3); 21 22 long startTimestamp, endTimestamp; 23 List<Long> commonA = null, commonB = null, commonC = null; 24 25 //First Attempt - Runs in 60 seconds 26 startTimestamp = System.currentTimeMillis(); 27 commonA = new ArrayList<Long>(listOf2); 28 commonA.retainAll(listOf3); 29 endTimestamp = System.currentTimeMillis(); 30 System.out.println("Execution Time : " + (endTimestamp-startTimestamp)/1000); 31 32 //Second Attempt - Runs in 73 seconds - 33 //There are fewer elements in commonB shouldn't this run faster? 34 startTimestamp = System.currentTimeMillis(); 35 commonB = new ArrayList<Long>(listOf3); 36 commonB.retainAll(listOf2); 37 endTimestamp = System.currentTimeMillis(); 38 System.out.println("Execution Time : " + (endTimestamp-startTimestamp)/1000); 39 40 System.out.println("Are Equal : " + (commonA.equals(commonB))); 41 42 //Third Attempt - Runs in 2 seconds 43 startTimestamp = System.currentTimeMillis(); 44 /* This part has been intentionally left blank. 45 * That is because I need to have a question for the puzzle, 46 * and I felt like leaving out the Third part would be the right 47 * thing do to. 48 * I've done most of the hard work so this should be easy to fill. 49 * Yes this must run 10 times faster than the solution I have provided 50 * and should be done just as many lines (3 to 5 lines of code should be fine!). 51 * Well I never said anything about life being fair, did I? 52 * */ 53 54 endTimestamp = System.currentTimeMillis(); 55 System.out.println("Execution Time : " + (endTimestamp-startTimestamp)/1000); 56 57 System.out.println("Are Equal : " + (commonA.equals(commonC))); 58 } 59 }