【发布时间】:2018-08-14 12:57:38
【问题描述】:
我在这里的第一篇文章,我对 java(以及任何编程)非常陌生,如下所示:)
我需要从列表中打印出一个单词,但是 由于某种原因,我的流命令在使用 filter() 后找不到参数(字符串字),即使该字存在于列表中。 (我尝试了没有过滤器的 sout() 整个列表,并在输入中找到了它)。
private List<String> lines;
public reviews(List<String> lines) {
this.lines = lines;
}
public void NumberOfWords(String theWord) {
lines.stream()
.map(lines -> lines.split(" "))
.map(words -> Arrays.toString(words))
.map(word -> word.trim().toLowerCase())
.filter(word -> word.equals(theWord)) // Something wrong with this line?
.forEach(word -> System.out.println(word));
}
如果没有流过滤器(),输出如下所示:
[1, a, series, of, escapades, proofing, the, adage, that, what, is, good, for, the, goose, is, also, good, for, the , gander, ,, some, of, which, 偶尔, 有趣, but, none, of, of, which, 相当于, much, of, a, story, . ] [4,这,安静,,,,内省,和,娱乐,独立,是,值得,寻求,。 ] [1, 甚至, 粉丝, of, ismail, 商人, 的, 工作, ,, 我, 怀疑, ,, will, have, a, hard, time, sitting, through, this, one, . ] [3、a、肯定//etc........
使用过滤器,假设我们将有参数词:“good”。它存在,但方法不打印它。
【问题讨论】:
-
为什么不打印出 each 流操作的结果,因为这样做肯定会显示您的错误假设?您的问题表明,将来您会想要在来这里之前做更多的调试。
-
@dbl 感谢您的回答!解释得很好,它对我理解问题有很大帮助:) 现在一切都清楚了。
标签: java filter java-stream