我正在使用程序來讀取文件中的關鍵字並根據頻率對它們進行排序。我使用相同的Map數據結構。 但我面臨的問題,即使我有重複的條目,他們的數量不增加。因此,如果在文件中重複一個單詞,它將存儲在數據結構的不同位置,而不是增加映射中先前條目的值。請在下面找到我的代碼。那是我的嘗試。另外我要確保一個匹配是「」還是「」它不存儲在哈希映射中,但它仍然被計數。插入哈希映射數據結構JAVA
Pattern p = Pattern.compile("[a-zA-Z]*",Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(handlerContent);
while(m.find()) //&& (m.group().length()>1)
{
boolean blnExists = keyword_counts.containsValue(m.group());
if(blnExists==true)
{
if(m.group()!="" || m.group()!=" ")
{
System.out.println("Repeat");
keyword_counts.put(m.group(), keyword_counts.get(m.group()+1));
System.out.println(m.group()+" "+keyword_counts.get(m.group()));
}
}
else
{
if(m.group()!="" || m.group()!=" ")
{
keyword_counts.put(m.group(), 1);
System.out.println(m.group()+" "+keyword_counts.get(m.group()));
}
}
}
謝謝主席先生,我已經修改了我的正則表達式來處理上述條件。 ContainsKey工作......我無視這一點是多麼的愚蠢 – CodeMonkey 2012-04-19 07:12:01