/* * Kyle Pierce * CSE 143 * * Client program making use of Lists, Sets, and Maps to glean * certain word-related information from "moby dick" (or "the whale"). */ import java.io.*; import java.util.*; public class WordCount { public static void main(String[] args) throws FileNotFoundException { /* * WARNING: This produces a lot of output. You probably want * to comment out certain parts to run them in isolation so you * can actually see the results more clearly. */ File f = new File("moby.txt"); Scanner input = new Scanner(f); long start = System.currentTimeMillis(); int count1 = countUniquesWithList(input); long stop = System.currentTimeMillis(); System.out.println("time with a list = " + ((stop - start) / 1000.0) + "s"); System.out.println("number of words = " + count1); System.out.println(); input = new Scanner(f); // effectively reset the Scanner start = System.currentTimeMillis(); int count2 = countUniquesWithSet(input); stop = System.currentTimeMillis(); System.out.println("time with a set = " + ((stop - start) / 1000.0) + "s"); System.out.println("number of words = " + count2); System.out.println(); input = new Scanner(f); // effectively reset the Scanner printCounts(input); } // post: returns the number of unique words in the given Scanner public static int countUniquesWithList(Scanner input) { List words = new ArrayList<>(); while (input.hasNext()) { String word = input.next(); if (!words.contains(word)) { words.add(word); } } return words.size(); } // post: returns the number of unique even-length words in the given // Scanner and prints them all out public static int countUniquesWithSet(Scanner input) { Set words = new HashSet<>(); while (input.hasNext()) { String word = input.next(); words.add(word); } // remove all the words with odd length // cannot use a for-each loop here, must be iterator Iterator itr = words.iterator(); while (itr.hasNext()) { String word = itr.next(); if (word.length() % 2 == 1) { // careful not to say words.remove(word) or we // would still get an exception itr.remove(); } } // print out all of the words, one per line for (String word : words) { System.out.println(word); } return words.size(); } // post: prints each word in the given Scanner followed by the number // of occurrences of that word public static void printCounts(Scanner input) { Map words = new TreeMap<>(); while (input.hasNext()) { String word = input.next(); // either add the word into the map for the first time // or update the existing count if already present if (!words.containsKey(word)) { words.put(word, 1); } else { int oldCount = words.get(word); words.put(word, oldCount + 1); } } // print out all the words and their # of occurrences for (String word : words.keySet()) { System.out.println(word + ": " + words.get(word) + " occurrences"); } } }