阿里给的一个笔试题,要我分析一个10G的文本,当时在线面试,没多想,随便写了写。现在自己写了一下,问题蛮多的。
我有三个4G的样本,都是我写程序随机生成的,字符集合不同,有的字符集很广,这样就会导致OOM
public class Solution {
map wordsCountmap = new Hashmap<>();
map computeWordsFromBufferReadFile(String filepath) {
File file = new File(filepath);
try (BufferedInputStream fis = new BufferedInputStream(new FileInputStream(filepath));
BufferedReader reader = new BufferedReader(new InputStreamReader(fis, "utf-8"), 5 * 1024 * 1024);) {// 用5M的缓冲读取文本文件 ?
String line = "";
while ((line = reader.readLine()) != null) {
computeWords(line);
}
line = null;
} catch (IOException e) {
e.printStackTrace();
}
return wordsCountmap;
}
//这个算法比上面的慢20%
map