2011-05-01 196 views
2

我想使用哈希表來計算文件中幾個字符串的出現次數。我會如何去做這件事?另外,我能夠以相似的方式計算唯一字符串的數量嗎?例子將不勝感激。Java哈希表實現

回答

6

作爲一個例子,下面是一個程序,它將讀取文件中的單詞並計算遇到Java關鍵字的次數。

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); 
    } 
} 

要回答關於唯一字符串的問題,可以採用類似方式來調整我使用HashMap的方式。

  1. 創建一個新的HashMap,稱之爲uniqueStrings
  2. 從文件中讀取字符串時,檢查,保持計數的軌道HashMap中包含當前字符串
    • 如果沒有,再加入它uniqueStrings
    • 如果確實如此,那麼從uniqueStrings
  3. 刪除它,你就大功告成了讀取文件後,你將只有唯一字符串uniqueStrings

如果您有任何問題,請告訴我。

我希望這會有所幫助。
Hristo

+0

謝謝,這是非常有幫助的。他們的關鍵字也來自文件,會使用StringTokenizer,然後將它們添加到HashMap的工作?我用了一些關於獨特琴絃的措辭。我需要做的是統計日誌文件中唯一IP地址的數量 - 或者說,檢查它是否已經存在於HashMap中,如果它沒有添加它,並且它確實不再添加它,以及最後統計HashMap中的IP地址數量。 – Terezi 2011-05-01 03:34:33

+0

我想通了,非常感謝:) – Terezi 2011-05-01 08:25:49

+0

@Terezi ......很高興我能幫到 – Hristo 2011-05-01 16:50:03

0

爲了跟蹤唯一字符串,您不需要跟蹤文件中出現的次數。相反,您可以使用HashSet代替HashMap以實現代碼清晰度。

注意:HashSet內部支持HashMap,最終對象用作鍵值對中的值。

+1

是的,但是一個Set只會有所有的唯一字符串,而OP要計算每個唯一字符串的出現次數。你打算如何用Set來做到這一點? – sharakan 2012-04-27 21:08:32