import java.io.File; import java.io.FileNotFoundException; import java.util.*; /** * Compares the runtime of using various data structures to * read in a large dictionary with duplicates. See the slides * for graphs of the results. * * @author Adam Blank */ public class WordCount { private static long start, stop; public static int timeUniqueList(List words, List ds) throws FileNotFoundException { start = System.currentTimeMillis(); for (int i = 0; i < words.size(); i++) { if (!ds.contains(words.get(i))) { ds.add(words.get(i)); } } return ds.size(); } public static int timeUniqueSet(List words, Set ds) throws FileNotFoundException { start = System.currentTimeMillis(); ds.addAll(words); return words.size(); } public static void main(String[] args) throws FileNotFoundException { List allWords = readFromFile("10000.txt"); int n = 50000; List words = allWords.subList(0, n); System.out.println("Doing " + words.size() + " words."); timeUniqueList(words, new ArrayList()); printResult("ArrayList", words.size()); timeUniqueList(words, new LinkedList()); printResult("LinkedList", words.size()); timeUniqueSet(words, new HashSet()); printResult("HashSet", words.size()); timeUniqueSet(words, new TreeSet()); printResult("TreeSet", words.size()); } public static void printResult(String type, int n) { stop = System.currentTimeMillis(); System.out.println(type + "(" + n + "): " + (stop - start) + "ms"); } public static List readFromFile(String filename) throws FileNotFoundException { List words = new ArrayList(); Scanner scanner = new Scanner(new File(filename)); while (scanner.hasNext()) { words.add(scanner.next()); } return words; } }