import java.util.*; public class WordCount { public static void main (String[] argv) { countWordsInFileUsingTree ("testfile"); countWordsInFileUsingList ("testfile"); } static void countWordsInFileUsingTree (String fileName) { // Create an instance of the data structure. BinaryTreeMap3 tree = new BinaryTreeMap3 (); // Read a text file and extract the words into an array. String[] words = WordTool.readWords (fileName); System.out.println ("Read in " + words.length + " words"); // Put words into data structure. If a word repeats, increment its count. for (int i=0; i { // We decide how to compare two key-value pairs. Java's sort // algorithm will repeatedly call this as it compares elements. public int compare (KeyValuePair kvp1, KeyValuePair kvp2) { // Note: the .value variable is of type Object. That's why we need the cast. Integer count1 = (Integer) kvp1.value; Integer count2 = (Integer) kvp2.value; if (count1 > count2) { return -1; } else if (count1 < count2) { return 1; } else { return 0; } } // We're required to implement this as part of implementing // the interface. public boolean equals (Object obj) { return false; } } //end-KeyValueComparator