赞
踩
public class topCharactorCounter { public static void main(String[] args) { File file = new File("E:\\1.txt"); int memoryLimit=1024*1024; int bufferSize=1024; Map<Character,Integer> chmap=new HashMap<>(); try { FileReader fileReader = new FileReader(file); BufferedReader bufferedReader=new BufferedReader(fileReader); String line; while ((line=bufferedReader.readLine())!=null){ if (!line.trim().isEmpty()){ for (char c:line.toCharArray()){ chmap.put(c,chmap.getOrDefault(c,0)+1); } } } } catch (Exception e) { throw new RuntimeException(e); } int topCount=5; for (int i = 0; i < topCount; i++) { char topChar=' '; int max=0; for (Map.Entry<Character,Integer> entry:chmap.entrySet()){ if (entry.getValue()>max){ max=entry.getValue(); topChar=entry.getKey(); } } System.out.print(topChar+":"+max+" "); chmap.remove(topChar); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。