Terezi
Terezi

Reputation: 33

Java Hashmap implementation

I want to use a hashmap to count the number of occurrences of several strings in a file. How would I go about doing this? Also, would I be able to count the number of unique strings in a similar fashion? Examples would be much appreciated.

Upvotes: 2

Views: 2017

Answers (2)

wuwc
wuwc

Reputation: 1

For tracking unique strings, you don't need to keep track of the number of occurrences in the file. Rather, you can use a HashSet instead of a HashMap for code clarity.

Note: HashSet is internally backed by a HashMap with a final object used as the Value in the Key Value pair.

Upvotes: 0

Hristo
Hristo

Reputation: 46547

As an example, here's a program that will read words from a file and count how many times a Java keyword was encountered.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;

public class CountKeywords {

    public static void main(String args[]) {

        String[] theKeywords = { "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "false", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "void", "volatile", "while" };

        // put each keyword in the map with value 0 
        Map<String, Integer> theKeywordCount = new HashMap<String, Integer>();
        for (String str : theKeywords) {
            theKeywordCount.put(str, 0);
        }

        FileReader fr;
        BufferedReader br;
        File file = new File(args[0]); // the filename is passed in as a String

        // attempt to open and read file
        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);

            String sLine;

            // read lines until reaching the end of the file
            while ((sLine = br.readLine()) != null) {

                // if an empty line was read
                if (sLine.length() != 0) {

                    // extract the words from the current line in the file
                    if (theKeywordCount.containsKey(sLine)) {
                        theKeywordCount.put(sLine, theKeywordCount.get(sLine) + 1);
                    }
                }
            }

        } catch (FileNotFoundException exception) {
            // Unable to find file.
            exception.printStackTrace();
        } catch (IOException exception) {
            // Unable to read line.
            exception.printStackTrace();
        } finally {
                br.close();
            }

        // count how many times each keyword was encontered
        int occurrences = 0;
        for (Integer i : theKeywordCount.values()) {
            occurrences += i;
        }

        System.out.println("\n\nTotal occurences in file: " + occurrences);
    }
}

To answer your question about unique strings, you can adapt the way I use the HashMap in a similar fashion.

  1. create a new HashMap, call it uniqueStrings
  2. when reading strings from the file, check if the HashMap that keeps track of the count contains the current string
    • if it doesn't, then add it to uniqueStrings
    • if it does, then remove it from uniqueStrings
  3. after you're done reading the file, you will have only unique strings in uniqueStrings

Let me know if you have questions.

I hope this helps.
Hristo

Upvotes: 6

Related Questions