Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a HashTable class (and any necessary helper classes): Separate chaining is

ID: 3865241 • Letter: W

Question

Write a HashTable class (and any necessary helper classes): Separate chaining is the strategy used to handle collision, which means the structure of the Hash Table looks like the picture below (see the Hash Table ADT lectures). The hash table size must be variable (not hard coded) according to user input, so that you can test with a variety of values. Keys will be the words from the input file and the values will be a count (of their occurrences). Method for converting a key to its hash code is up to you, but a simple option is to sum the UNICODE VALUE of each character. You may wish to enhance the calculation by accounting for the position of each character in the key. Make sure to account for case (i.e., do you consider upper and lower case to be equivalent?). You must have separate methods for calculating hashCode() and hashlndex(). For example: As usual, provide constructors and mutate and accessory methods for all instance variables, plus toArray() and toString() methods. Your unit testing (use main() and utility methods in HashTable.java) must be thorough - test all methods.

Explanation / Answer

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

public class WCount {

public static void main(String args[]) {
Map<String, Integer> wmap = WordMapping("C:/gutten.txt");
List<Entry<String, Integer>> lst = sortDecrease(wmap);
System.out.println("List of repeated word from file and their count");
for (Map.Entry<String, Integer> entry : lst) {
if (entry.getValue() > 1) {
System.out.println(entry.getKey() + " => " + entry.getValue());
}
}
}

public static Map<String, Integer> WordMapping(String fn) {
Map<String, Integer> wmap = new HashMap<>();
try (FileInputStream flIpStream = new FileInputStream(fn);
DataInputStream data = new DataInputStream(flIpStream);
BufferedReader buff = new BufferedReader(new InputStreamReader(data))) {
Pattern patt = Pattern.compile("\s+");
String str = null;
while ((str = buff.readLine()) != null) {
str = str.toLowerCase();
String[] wrd = patt.split(str);
for (String word : wrd) {
if (wmap.containsKey(word)) {
wmap.put(word, (wmap.get(word) + 1));
} else {
wmap.put(word, 1);
}
}
}
} catch (IOException ioex) {
ioex.printStackTrace();
}
return wmap;
}

public static List<Entry<String, Integer>> sortDecrease(Map<String, Integer> wmap) {
Set<Entry<String, Integer>> entry = wmap.entrySet();
List<Entry<String, Integer>> lst = new ArrayList<>(entry);
Collections.sort(lst, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> obj, Map.Entry<String, Integer> obj1) {
return (obj1.getValue()).compareTo(obj.getValue());
}
});
return lst;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote