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

Objective: Hash table access using java.util.HashMap Consider the problem of cou

ID: 3776762 • Letter: O

Question

Objective: Hash table access using java.util.HashMap

Consider the problem of counting the number of occurrences of different words in a document, a hash table is an ideal data structure to use here, for we can use words as keys and word counts as value.

Download Entry.java and document.txt .

Use java.util.HashMap to write a java program to find out the maximum word count in the input document.txt file. At the end, display the word count of the word you found.

Given Code:

//********************************************************************
// Entry.java
//
//********************************************************************

public class Entry
{
protected String word;
protected Integer count;

//-----------------------------------------------------------------
// Constructor: Sets up entry
//-----------------------------------------------------------------
public Entry(String w, Integer c) {
word = w;
count = c;
}

  
public String getWord()
   {
       return word;
   }

public int getCount()
{
return count;

}
//-----------------------------------------------------------------
// Returns a string including the basic employee information.
//-----------------------------------------------------------------
public String toString()
{
String result = "Word: " + word + " ";

result += "count: " + count;

return result;
}

//-----------------------------------------------------------------
//-----------------------------------------------------------------

}

Text Doc:

When considering choice of data structure use in database
system that manages customer accounts, we see that a data structure that
has little concern for cost of deletion, but is highly efficient for search
and moderately efficient for insertion, should meet resource constraints
imposed by this problem. Records are accessible by unique account number
(sometimes called an exact-match query). One data structure that meets
se requirements is hash table described in Chapter 9.4. Hash tables
allow for extremely fast exact-match search. A record can be modified
quickly when modification does not affect its space requirements. Hash
tables also support efficient insertion of new records. While deletions can
also be supported efficiently,o many deletions lead some degradation
in performance for remaining operations. However, hash table can
be reorganized periodically restore system peak efficiency. Such
reorganization can occur offline so as not affect ATM transactions.

Explanation / Answer

import java.io.*;
import java.util.*;

public class Entry {
   
    public Map<String, Integer> getWordCount(String fileName){

        FileInputStream fis = null;
        DataInputStream dis = null;
        BufferedReader br = null;
        Map<String, Integer> wordMap = new HashMap<String, Integer>();
        try {
            fis = new FileInputStream(fileName);
            dis = new DataInputStream(fis);
            br = new BufferedReader(new InputStreamReader(dis));
            String line = null;
            while((line = br.readLine()) != null){
                StringTokenizer st = new StringTokenizer(line, " ");
                while(st.hasMoreTokens()){
                    String tmp = st.nextToken().toLowerCase();
                    if(wordMap.containsKey(tmp)){
                        wordMap.put(tmp, wordMap.get(tmp)+1);
                    } else {
                        wordMap.put(tmp, 1);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try{if(br != null) br.close();}catch(Exception ex){}
        }
        return wordMap;
    }
   
    public List<Entry<String, Integer>> sortByValue(Map<String, Integer> wordMap){
       
        Set<Entry<String, Integer>> set = wordMap.entrySet();
        List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
        Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
        {
            public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
            {
                return (o2.getValue()).compareTo( o1.getValue() );
            }
        } );
        return list;
    }
   
    public static void main(String a[]){
        Entry wordcount = new Entry();
        Map<String, Integer> wordMap = wordcount.getWordCount("C:\Users\sontosh\Desktop\document.txt");
        List<Entry<String, Integer>> list = wordcount.sortByValue(wordMap);
        for(Map.Entry<String, Integer> entry:list){
            System.out.println(entry.getKey()+" ==== "+entry.getValue());
        }
    }
}

i svaed the dcument on desktop

Output is like below

for ==== 5
hash ==== 4
that ==== 4
can ==== 4
in ==== 3
be ==== 3
efficient ==== 3
data ==== 3
of ==== 3
structure ==== 3
when ==== 2
tables ==== 2
is ==== 2
system ==== 2
affect ==== 2
by ==== 2
table ==== 2
a ==== 2
exact-match ==== 2
deletions ==== 2
not ==== 2
also ==== 2
moderately ==== 1
called ==== 1
quickly ==== 1
periodically ==== 1
constraints ==== 1
concern ==== 1
number ==== 1
extremely ==== 1
record ==== 1
efficiently,o ==== 1
accounts, ==== 1
meets ==== 1
considering ==== 1
an ==== 1
remaining ==== 1
as ==== 1
unique ==== 1
accessible ==== 1
chapter ==== 1
offline ==== 1
see ==== 1
search ==== 1
se ==== 1
are ==== 1
9.4. ==== 1
does ==== 1
however, ==== 1
reorganization ==== 1
so ==== 1
problem. ==== 1
(sometimes ==== 1
reorganized ==== 1
manages ==== 1
1
many ==== 1
lead ==== 1
records. ==== 1
such ==== 1
fast ==== 1
meet ==== 1
operations. ==== 1
choice ==== 1
requirements. ==== 1
supported ==== 1
but ==== 1
imposed ==== 1
use ==== 1
degradation ==== 1
while ==== 1
space ==== 1
should ==== 1
modified ==== 1
has ==== 1
atm ==== 1
described ==== 1
new ==== 1
resource ==== 1
occur ==== 1
this ==== 1
its ==== 1
modification ==== 1
insertion ==== 1
support ==== 1
deletion, ==== 1
little ==== 1
allow ==== 1
some ==== 1
records ==== 1
we ==== 1
transactions. ==== 1
database ==== 1
search. ==== 1
and ==== 1
highly ==== 1
query). ==== 1
requirements ==== 1
cost ==== 1
restore ==== 1
peak ==== 1
efficiency. ==== 1
performance ==== 1
insertion, ==== 1
account ==== 1
customer ==== 1