// Helene Martin, CSE 143 // 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 1528 4122 // LinkedList 2785 6816 // TreeSet 374 454 // HashSet 332 450 public class WordCount { public static void main(String[] args) throws FileNotFoundException { // List words = new LinkedList(); Set words = new HashSet(); 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 Set 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); } } }