【发布时间】:2020-04-16 15:04:56
【问题描述】:
我想问如何提高代码性能?我需要获取所有 html 代码并将其保存在 Queue-LinkedList 中。但是在提取过程中,我使用了loop里面的loop O(n^2)。这太慢了。如何改进这段代码?
公共类 ParsingHtml {
private static Queue<Character> queueCharacter = new LinkedList<>();
public static void downloadHtmlCode(String addressUrl) throws IOException {
InputStream is = null;
try (BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(is = (new URL(addressUrl)).openStream()))) {
bufferedReader.lines()
.filter(str -> !str.isEmpty())
.forEach(str -> {
for (char ch : str.toCharArray())
if (ch != ' ') queueCharacter.add(ch);
});
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
is.close();
}
}
【问题讨论】: