// This program counts the number of unique words in a large file. // This program calculates the number of unique words in a file // and prints out information about how long it took to run s import java.io.*; import java.util.*; public class UniqueWords { public static void main(String[] args) throws FileNotFoundException { System.out.println("Reading file..."); Scanner input = new Scanner(new File("mobydick.txt")); long start = System.currentTimeMillis(); int uniqueWords = countUnique(input); long end = System.currentTimeMillis(); long elapsed = end - start; System.out.println("The file has " + uniqueWords + " words."); System.out.println("Took " + elapsed + " ms."); } // pre: input is not null // post: returns the number of unique tokens in input public static int countUnique(Scanner input) { // List words = new ArrayList(); // Set words = new Treeset(); Set words = new HashSet(); while (input.hasNext()) { String word = input.next(); if (!words.contains(word)) { words.add(word); } } return words.size(); } // pre: words is not null // post: prints the words in the given list that have even length. private static void printEvenLengthWords(List words) { // Hunter: Changed the parameter to type List so I am always using the interface type // Confusing differences in how to get how many elements are contained // arrays => length // Strings => length() // ArrayList => size() /* Hunter: for loop, only works for things with indices (lists, arrays) * for (int i = 0; i < words.size(); i++) { * String word = words.get(i); * * if (word.length() % 2 ==0 ){ * System.out.println(word); * } * } */ /* Hunter: foreach loop, works for pretty much all collections (including arrays) * Note: only works for read-only operations, can't remove */ for (String word : words) { if (word.length() % 2 ==0 ){ // words.remove(word); Causes a ConcurrentModificationException System.out.println(word); } } /* Hunter: iterator, works for pretty much all collections (excluding arrays) * Iterator interface * hasNext(), returns true if there are more elements to return * next(), returns the next thing in the list (advances the iterator forward) * remove(), removes the last thing returned by a call to next() * * Iterator itr = words.iterator(); * while (itr.hasNext()) { * String word = itr.next(); * if (word.length() % 2 ==0 ){ * itr.remove(); * // words.remove(word); Causes a ConcurrentModificationException * System.out.println(word); * } * } */ } }