【发布时间】:2014-04-10 12:24:18
【问题描述】:
我正在阅读一个简单的记事本文本文件,其中包含许多实际大小为 3mb 的数据,因此您可以想象它可以包含的字数!问题是我正在将该文件读入一个字符串,然后拆分该字符串,以便我可以将每个单词保存在 ArrayList(String) 中。它对我来说很好,但实际问题是我出于某种目的处理这个数组列表,然后我必须再次追加,或者你可以说将数组列表的所有单词放回字符串!
所以步骤是:
- 我将一个文本文件读入一个字符串(全文本)
- 将所有单词拆分成一个数组列表
- 处理该数组列表(假设我删除了所有停用词,例如 is、am、are)
- 在对数组列表进行处理后,我想将数组列表的所有单词放回字符串(alltext)
- 那么我必须使用该字符串(全文本) (alltext是所有处理后必须包含文本的字符串)
问题在于,在第 4 步,将所有单词附加回我的代码是:
BufferedReader br = new BufferedReader(new FileReader(file));
String line = "";
while ((line = br.readLine()) != null) {
alltext += line.trim().replaceAll("\\s+", " ") + " ";
}
br.close();
//Adding All elements from all text to temp list
ArrayList<String> tempList = new ArrayList<String>();
String[] array = alltext.split(" ");
for (String a : array) {
tempList.add(a);
}
//remove stop words here from the temp list
//Adding File Words from List in One String
alltext = "";
for (String removed1 : tempList) {
System.out.println("appending the text");
alltext += removed1.toLowerCase() + " ";
//here it is taking a lot of time suppose 5-10 minutes for a simple text file of even 1.4mb
}
所以我只是想知道任何想法,以便我可以减少有效处理的时间并放松机器!我会感谢任何建议和想法... 谢谢
【问题讨论】:
-
为什么要创建和使用单独的
List?使用数组本身。 -
您是否分析过您的代码以准确找出哪个循环花费的时间最多?
-
for (String removed1 : tempList) 这段代码需要很多时间
-
并使用
StringBuilder而不仅仅是连接。
标签: java arrays regex string arraylist