【问题标题】:adding new strings to a hashmap java将新字符串添加到哈希图 java
【发布时间】:2013-03-12 17:23:31
【问题描述】:

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

public class Logs
{

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

 Map<String, Integer> dayCount = new HashMap<String, Integer>();
    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));

    }

    }
   }
}

}

【问题讨论】:

  • 你的意思是你不知道放什么? ....把你放在其他地方的东西放在那里。
  • 什么是DayCount?它是如何以及在哪里定义的?
  • DayCount 是标准的HashMap 吗?
  • DayCount 是我的哈希图,定义为Map&lt;String, Integer&gt; DayCount = new HashMap&lt;String, Integer&gt;();
  • @user200784:类名使用 PascalCase,变量和方法使用 camelCase。

标签: java hashmap indexof


【解决方案1】:

您无需遍历 hashmap 的键来查看是否存在!这违背了使用哈希图的目的(在您的解决方案中查找O(1) 没有冲突与O(n))。你应该只需要做这样的事情:

//If a key doesn't exist in a hashmap, `get(T)` returns null
if(DayCount.get(str) == null) {
    //We know this key doesn't exist, so let's create a new entry with 1 as the count
    DayCount.put(str, 1);
} else {
    //We know this key exists, so let's get the old count, increment it, and then update
    //the value
    int count = DayCount.get(str);
    DayCount.put(str, count + 1);
}

另一方面,请考虑遵循 Java 命名约定。变量应以小写字母开头(即dayCountDayCount)。只有类应该以大写字母开头。按照您现在的方式,DayCount 是一个带有名为 put 的静态方法的类。

【讨论】:

  • @Vivian Paliath 嗯好的,但应该将 str 定义为好像我没有在任何东西上循环它?
  • @user2007843 我只是使用str 作为占位符。您必须从从文件中读取的行中取出单个字符串(基本上拆分 sCurrentLine)。我假设您正在尝试计算单个单词或类似的东西。
  • @Vivian Paliath 我计算的一个例子是"ua, login, reboot"
  • @user2007843 然后您可以拆分sCurrentLine 并检查各个令牌以构建您的地图。
  • @Vivian Paliath 所以我添加了这个,但在字符串类型和迭代内容方面遇到了问题。 for (String str : keyWords ) //this isnt working { str = sCurrentLine.split("-- "); if (str.contains("ua, ")) { //then do what you added
【解决方案2】:

既然这是你的要求 -

it should create a new string from "ua, " to the end of the line

由于不清楚一行是以“ua”还是“ua”开头的,因此可能位于该行的中间。这大概就是它的样子——

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

        if( sCurrentLine.indexOf("ua, ") != -1 ){
             String str = sCurrentLine.substr("ua, ");
             if(dayCount.get(str) != null){
                  dayCount.put(str, dayCount(str) +1 );
              }else{
                  dayCount.put(str, 1 ); 
              }
        }

    }

【讨论】:

    猜你喜欢
    • 2016-09-06
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多