// CSE 143, Winter 2011, Marty Stepp // 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.*; // arraylist smallmoby 2.6s mobydick 7.2s bible 16.7s // treeset smallmoby 0.47s mobydick 0.63s bible 1.3s public class WordCount { public static void main(String[] args) throws FileNotFoundException { Set words = new TreeSet(); long start = System.currentTimeMillis(); System.out.println("Reading file..."); Scanner input = new Scanner(new File("smallmoby.txt")); while (input.hasNext()) { String word = input.next(); // if (!words.contains(word)) { 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."); // System.out.println("words: " + words); // for (String word : words) { // System.out.println(word); // words.remove("the"); // } } }