【发布时间】:2015-06-25 00:02:43
【问题描述】:
在我的代码中
Files.lines(Paths.get(fileName), Charset.forName("Cp1252"))
.filter(k -> k != "")
.forEach(m -> hashmap.put(LocalDateTime.MIN, m));
从名为“info”(fileName) 的文件中读取行。
1
2
3
4
5
(blank line)
但是,当扫描 HashMap 时,它告诉我只使用此代码插入了最后一行:
int x = 0;
for (HashMap.Entry<LocalDateTime, String> s : hashmap.entrySet()) {
x++;
System.out.println(hashmap.size() + ": " + x + ": " + s.getValue());
}
1: 1: 5 只打印一次。
【问题讨论】:
-
.filter(k -> k != "")不要将字符串与==或!=进行比较。使用equals(或否定!结果)。 -
@Pshemo 错过了,谢谢。
-
…或者只使用
.filter(k -> ! k.isEmpty())…
标签: java hashmap java-8 java-stream