2011-05-01 106 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