Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
 

Using a Map to Count Words


by MageLang Institute

[Magercise | API Docs | Short Course| Magercises]

Help is available for each task.



    Task 1

    Either start with the skeleton code or create a WordCount class.


    Shift click to save the file to your working directory.

    If you don't start from the skeleton code, you'll have to read the URL yourself and parse the contents with a StringTokenizer.

    Task 2

    Create an instance variable of type Map and assign it to a new HashMap. In the getMap() method return the map created. In the clear() method, empty out the defined map.


    Use the clear() method of Map to empty it out.

    Task 3

    Complete the addWords() method to count each word returned by the StringTokenizer. The program already separates each line in the URL into individual words.


    The value for the key (word) is the current frequency. If the word is not found, then it is not in the map yet and should be added with a value of one. Otherwise, add one to the existing frequency.

    Refer back to the MapExample program from the course notes for specifics.

    Feel free to alter try a different set of delimeters with the StringTokenizer.

    Task 4

    The Tester program has a JTextArea to display the results of the counting. The program displays the String returned by the private convertMap() method in the JTextArea. It is your job to format the output nicely, as the toString() of AbstractMap displays everything on one line. Start off with the skeleton code for CaseInsensitiveComparator so you can sort the output in a case insensitive manner. The implementation will be identical to the course notes.


    Shift click to save the file to your working directory.

    Either implement compare() yourself, or copy it from the course notes.

    Task 5

    Now that you have a case-insentive Comparator, use it to create a TreeMap full of the original map contents. That way, the output can be displayed sorted.


    Getting the original Map sorted with the new Comparator is a two step process. In the TreeMap constructor, specify the Comparator. Then, put all the original map entries in the TreeMap with the putAll() method.

    Task 6

    After getting an Iterator of all the keys, display out one key-value pair per line. Using the predefined PrintWriter to format the output. It is backed by a StringBuffer and will be automatically returned.


    First get the entry set with the entrySet() method.

    Then, get its Iterator. Each element is a Map.Entry

    Task 7

    Compile your program and run the Tester program to see what happens. You specify the URL to read in the JTextField. When you press ENTER, the URL is read and the words are added to the map. Once done reading, the JTextArea is updated. If you ever wish to clear out the map, press the Clear button.

Copyright © 1999 MageLang Institute. All Rights Reserved.


[ This page was updated: 29-Sep-99 ]

Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-99 Sun Microsystems, Inc.
All Rights Reserved. Legal Terms. Privacy Policy.