DEV Community

Cover image for Case Study: Occurrences of Words
Paul Ngugi
Paul Ngugi

Posted on

Case Study: Occurrences of Words

This case study writes a program that counts the occurrences of words in a text and displays the words and their occurrences in alphabetical order of the words. The program uses a TreeMap to store an entry consisting of a word and its count. For each word, check whether it is already a key in the map. If not, add an entry to the map with the word as the key and value 1. Otherwise, increase the value for the word (key) by 1 in the map. Assume the words are case insensitive; e.g., Good is treated the same as good.

The code below gives the solution to the problem.

Image description

a 2
class 1
fun 1
good 3
have 3
morning 1
visit 1

The program creates a TreeMap (line 11) to store pairs of words and their occurrence counts. The words serve as the keys. Since all values in the map must be stored as objects, the count is wrapped in an Integer object.

The program extracts a word from a text using the split method (line 13) in the String class. For each word extracted, the program checks whether it is already stored as a key in the map (line 18). If not, a new pair consisting of the word and its initial count (1) is stored in the map (line 19). Otherwise, the count for the word is incremented by 1 (lines 21–23).

The program obtains the entries of the map in a set (line 29), and traverses the set to display the count and the key in each entry (lines 32–33).

Since the map is a tree map, the entries are displayed in increasing order of words. You can display them in ascending order of the occurrence counts as well.

Now sit back and think how you would write this program without using map. Your new program would be longer and more complex. You will find that map is a very efficient and powerful data structure for solving problems such as this.

Top comments (0)