我必须计算单词的出现次数并按出现次数(按降序)显示它们。我可以数单词,但不知道如何进一步进行。可能是我没有有效地做到这一点。如果可能,请提出解决方案。
import java.io.*;
import java.util.*;
public class MaxOccurence
{
public static void main(String[] args)
{
Map<String, Integer> map = new HashMap<>();
try
{
BufferedReader br = new BufferedReader(new FileReader(new File(
"F:/Demo/file_reading/bin/demo/hello.txt")));
String str;
while ((str = br.readLine()) != null)
{
Scanner sc = new Scanner(str);
while (sc.hasNext())
{
String word = sc.next();
if (map.containsKey(word))
map.put(word, map.get(word) + 1);
else
map.put(word, 1);
}
}
System.out.println("yes");
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println(map);
}
}