I am using a program to read keywords in a file and sort them on basis of frequency. I am using Map data structure for the same.
But i am facing problem, even if i have repeated entries, their count is not increased. Thus if a word is repeated in the file it is stored in a different place in the data structure rather than incrementing value of the previous entry in the maps. Please find my code below. That is what i tried. Also i am making sure if a match is “” or ” ” it is not stored in the hash map still it is counted.
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()) );
}
}
}
Your test should probably be:
And you should use
equalsto compare strings instead of==or!=but that is not the reason for your issue.