java hashmap 添加_将新字符串添加到hashmap java

我正在编写一个程序来读取日志文件,然后计算某些字符串的显示次数.我试图手动输入字符串作为关键字,但由于有这么多,我认为搜索日志文件会更好,当遇到“ua”时,它应该创建一个从“ua”到该行的结尾,将其添加到hashmap,并增加该特定字符串的计数(我感兴趣的所有字符串都以“ua”开头).我似乎无法弄清楚如何将新字符串添加到hashmap中.这就是我到目前为止所拥有的.

public class Logs

{

public static void main(String args[]) throws IOException

{

Map dayCount = new HashMap();

for (String str : KeyWords)

{

dayCount.put(str, 0);

}

File path = new File("C:\\P4logs");

for(File f: path.listFiles())

{ // this loops through all the files + directories

if(f.isFile())

{ // checks if it is a file, not a directory.

try (BufferedReader br = new BufferedReader(new FileReader(f.getAbsolutePath())))

{

String sCurrentLine;

while ((sCurrentLine = br.readLine()) != null)

{

boolean found = false;

for (String str : DayCount.keySet())

{

if (sCurrentLine.indexOf(str) != -1)

{

DayCount.put(str, DayCount.get(str) + 1);

found = true;

break;

}

}

if (!found && sCurrentLine.indexOf("ua, ") != -1)

{

System.out.println("Found an unknown user action: " + sCurrentLine);

DayCount.put(key, value) //not sure what to put here

}

}

}

for(String str : KeyWords)

{

System.out.println(str + " = " + DayCount.get(str));

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值