Java Hashmap实现
我想使用hashmap来计算文件中几个字符串的出现次数。我该怎么做呢?另外,我是否能够以类似的方式计算唯一字符串的数量?示例将不胜感激。作为示例,下面是一个程序,它将从文件中读取单词,并计算遇到Java关键字的次数Java Hashmap实现,java,hashmap,Java,Hashmap,我想使用hashmap来计算文件中几个字符串的出现次数。我该怎么做呢?另外,我是否能够以类似的方式计算唯一字符串的数量?示例将不胜感激。作为示例,下面是一个程序,它将从文件中读取单词,并计算遇到Java关键字的次数 import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOExceptio
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);
}
}
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.Map;
导入java.util.HashMap;
公共类关键字{
公共静态void main(字符串参数[]){
字符串[]关键字={“抽象”、“断言”、“布尔”、“中断”、“字节”、“大小写”、“捕获”、“字符”、“类”、“常量”、“继续”、“默认”、“do”、“双精度”、“else”、“枚举”、“扩展”、“false”、“final”、“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”};
//在地图中放置值为0的每个关键字
Map theKeywordCount=newhashmap();
for(字符串str:theKeywords){
关键字count.put(str,0);
}
文件阅读器fr;
缓冲剂;
File File=new File(args[0]);//文件名作为字符串传入
//尝试打开并读取文件
试一试{
fr=新文件读取器(文件);
br=新的缓冲读取器(fr);
弦线;
//读取行,直到到达文件末尾
而((sLine=br.readLine())!=null){
//如果读取了空行
如果(sLine.length()!=0){
//从文件中的当前行中提取单词
if(关键字count.containsKey(sLine)){
关键字计数.put(sLine,关键字计数.get(sLine)+1);
}
}
}
}捕获(FileNotFoundException异常){
//找不到文件。
异常。printStackTrace();
}捕获(IOException异常){
//无法读取行。
异常。printStackTrace();
}最后{
br.close();
}
//计算每个关键字被重新输入的次数
int=0;
for(整数i:theKeywordCount.values()){
事件+=i;
}
System.out.println(“\n\n文件中的总发生次数:“+发生次数”);
}
}
为了回答您关于唯一字符串的问题,您可以以类似的方式调整我使用HashMap的方式
uniqueStrings
- 如果没有,则将其添加到
uniqueStrings
- 如果是,则将其从
uniqueStrings
uniqueStrings
Hristo对于跟踪唯一字符串,您不需要跟踪文件中出现的次数。相反,为了代码清晰,您可以使用
哈希集
而不是哈希映射
注意:
HashSet
由一个HashMap
内部支持,最后一个对象用作键值对中的值。谢谢,这非常有用。它们也是来自文件的关键字,使用StringTokenizer然后将它们添加到HashMap是否有效?我对唯一字符串使用了糟糕的措辞。我需要做的是计算日志文件中唯一IP地址的数量——或者更确切地说,检查HashMap中是否已经存在该地址,如果不存在,则添加该地址,如果不再次添加该地址,最后计算HashMap中的IP地址数量。是的,但是一个集合将只包含所有唯一字符串,OP希望计算每个uniq的出现次数ue字符串。你建议如何使用一组?