The program is to count the frequency of words in the above file. Basically, if the word shows up for the first time we count it as being 1. If we have encountered already we increment the word count by 1. We would like to print the words in the files and count how many of them we read.Hint: Use the following loop to print the output as follows: for(String word : wordCount.keySet()) System.out.println(word + " " + wordCount.get(word));

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

(Count entire words, not parts of words. For example rate and rated would be different counts. Say rate is there 10 times and rated is there 5 times, that should be the count not rate 15, rated 5. Don't count parts of words.)

 

A Map is an interface that maps keys to values. The keys are unique and thus, no duplicate keys are allowed. A map can provide three views, which allow the contents of the map to be viewed as a set of keys, collection of values, or set of key-value mappings. In addition, the order of the map is defined as the order in which, the elements of a map are returned during iteration.

 

The Map interface is implemented by different Java classes, including HashMap, HashTable, and TreeMap. Each class provides different functionality and can be either synchronized or not. Also, some implementations prohibit null keys and values, and some have restrictions on the types of their keys.

 

A map has the form Map <k,v> where:

 

K: specifies the type of keys maintained in this map.

V: defines the type of mapped values.

Furthermore, the Map interface provides a set of methods that must be implemented. In this section, we will discuss about the most famous methods:

clear: Removes all the elements from the map.

containsKey: Returns true if the map contains the requested key.

containsValue: Returns true if the map contains the requested value.

equals: Compares an Object with the map for equality.

get: Retrieve the value of the requested key.

entrySet: Returns a Set view of the mappings contained in this map.

keySet: Returns a Set  that contains all keys of the map.

put: Adds the requested key-value pair in the map.

remove: Removes the requested key and its value from the map, if the key exists.

size: Returns the number of key-value pairs currently in the map.

 

Of interest to us is the TreeMap. Here is an example of TreeMap with a Map:

 

 

import java.util.Map;

import java.util.TreeMap;

 

public class TreeMapExample {

    public static void main(String[] args) {

        Map<String, Integer> vehicles = new TreeMap<>();

 

        // Add some vehicles.

        vehicles.put("BMW", 5);

        vehicles.put("Mercedes", 3);

        vehicles.put("Audi", 4);

        vehicles.put("Ford", 10);

      System.out.println("Total vehicles: " + vehicles.size());

 

        // Iterate over all vehicles, using the keySet method.

        for (String key : vehicles.keySet())

            System.out.println(key + " - " + vehicles.get(key));

        System.out.println();

 

        System.out.println("Highest key: " + ((TreeMap) vehicles).lastKey());

        System.out.println("Lowest key: " + ((TreeMap) vehicles).firstKey());

 

        System.out.println("\nPrinting all values:");

 

        for (Integer val : vehicles.values())

            System.out.println(val);

        System.out.println();

 

        // Clear all values.

        vehicles.clear();

 

        // Equals to zero.

        System.out.println("After clear operation, size: " + vehicles.size());

    }

}

 

The suspected output looks like this:

 

Total vehicles: 4

Audi - 4

BMW - 5

Ford - 10

Mercedes - 3

 

Highest key: Mercedes

Lowest key: Audi

 

Printing all values:

4

5

10

3

 

After clear operation, size: 0

Press any key to continue . . .

 

 

Assume that we have an actual FactBook2008.txt which you need to read. We want to create a wordcount map made out of K=String, and V=Integer. The program is to count the frequency of words in the above file. Basically, if the word shows up for the first time we count it as being 1. If we have encountered already we increment the word count by 1.

We would like to print the words in the files and count how many of them we read.Hint: Use the following loop to print the output as follows:

for(String word : wordCount.keySet())
System.out.println(word + " " + wordCount.get(word));

 

 

Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

I need to print the map size also. Any idea how to do that?

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Basics of loop
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education