我明白为什么下面的代码是可以的。因为集合在调用终端操作之前正在修改。java 8流干扰与非干扰
List<String> wordList = ...;
Stream<String> words = wordList.stream();
wordList.add("END"); // Ok
long n = words.distinct().count();
但为什么这段代码不好?
Stream<String> words = wordList.stream();
words.forEach(s -> if (s.length() < 12) wordList.remove(s)); // Error—interference
Thx。我不知道这是一个终端操作。 –