// Allison Obourn // CSE 143 - lecture 11 // This program counts the number of words in a large file. // It demonstrates the use of a Set collection. import java.io.*; import java.util.*; // time taken in ms // smallmoby mobydick // ArrayList 5699 16016 // LinkedList 10967 31887 // TreeSet 1101 1459 // HashSet 1040 1342 public class WordCount { public static void main(String[] args) throws FileNotFoundException { //List words = new ArrayList(); Set words = new TreeSet(); System.out.println("Reading file..."); //Scanner input = new Scanner(new File("smallmoby.txt")); Scanner input = new Scanner(new File("mobydick.txt")); long start = System.currentTimeMillis(); while (input.hasNext()) { String word = input.next(); //if(!words.contains(word)) { // not needed for sets words.add(word); //} } long end = System.currentTimeMillis(); long elapsed = end - start; System.out.println("The file has " + words.size() + " words."); System.out.println("Took " + elapsed + " ms."); // display contents of set. Can't use a regular for loop // since sets don't have indexes for (String word : words) { System.out.println(word); } } }