// Allison Obourn // CSE 143 - lecture 2 // Counts the occurrences of words in a large file and prints the most // common ones in order of popularity. // It demonstrates the use of a Map collection. import java.io.*; import java.util.*; public class WordCount2 { public static void main(String[] args) throws FileNotFoundException { System.out.println("Reading file..."); Scanner input = new Scanner(new File("mobydick.txt")); Map words = new TreeMap(); while(input.hasNext()) { String word = input.next().toLowerCase(); // when we get the value of a key that's not there, // we get null! Adding 1 to null causes an exception. // we therefore need to separate the cases where the key is already // in the map from the one where it's not. if(words.containsKey(word)) { words.put(word, words.get(word) + 1); } else { words.put(word, 1); } } // create a reversed version of the map so we can order by count Map> counts = new TreeMap>(); for(String word : words.keySet()) { int count = words.get(word); if(count > 40) { if(!counts.containsKey(count)) { counts.put(count, new HashSet()); } counts.get(count).add(word); } } // iterate over the map using the set of keys for(int count : counts.keySet()) { System.out.println(count + " " + counts.get(count)); } } }