package lab09; import java.io.FileInputStream; import java.util.*; public class HashClient { // The hashKey for this lab is an Integer given by sum of chars in str. // In hw5: hashKey is a String with letters in str sorted alphabetically public static Integer getLabHashKey(String str) { Integer hashKey = 0; for( int i = 0; i < str.length(); i++ ) hashKey += str.charAt(i); return hashKey; } public static void main(String [] args) throws Exception { // HashTable array below stores reference to WordList chains // TASK: Change LHS in the following statement to use generic version of WordList class. // Note: Do not change the RHS to use generic. JAVA seems to have a bug that throws an error // when creating a generic array, hence leave the RHS unchanged (only for this line). WordList [] hashTable = new WordList[20]; // Initialize hashTable[ 0,...,99 ] to empty WordLists for( int j = 0; j < hashTable.length; j++ ) // TASK: Change the following line to use generic version of WordList class hashTable[j] = new WordList(); // Create a Scanner object to read from TWL file Scanner scan = new Scanner(new FileInputStream("Z:\\TWL.txt")); while( scan.hasNext() ){ String curStr = scan.next(); // curKey below is an Integer for today's lab purposes // In hw5: curKey is the alphagram of curStr and should be a String Integer curKey = getLabHashKey(curStr); // Create a new Entry instance // TASK: Change the following line to use generic version of Entry class Entry newEntry = new Entry(curKey, curStr); // For lab purposes we use curKey to get hashBucket int hashBucket = curKey % hashTable.length; hashTable[hashBucket].addToHead(newEntry); } // Now use the wordList iterator to print 10 items from each list for( int j = 0; j < hashTable.length; j++ ) { // Get the iterator for the j'th word list // TASK: Change the following line to use generic version of Entry class Iterator> wordListIterator = hashTable[j].iterator(); // Use the iterator to print 10 items int k = 0; while( wordListIterator.hasNext() && k < 10 ){ // Prints ten (hashKey, dataValue) pairs for each wordList System.out.print("("+ wordListIterator.next().getKey() + "," + wordListIterator.next().getValue() + ") "); k++; } System.out.println(); } } }