在使用字谜的练习中,我发现执行后的代码在输出中有重复项,具体取决于类中方法的使用时刻。sorted()Stream
当我在做排序之前和方法filter()forEach()
words.stream()
.sorted()
.filter(s1 -> !alreadyFound.contains(s1) && words.stream()
.filter(s2 -> isAnagram.apply(s1, s2))
.count() == maxAnagrams)
.forEach(s1 -> {
它给出了这些结果:
abel able bale bela elba
alger glare lager large regal
angel angle galen glean lange
caret carte cater crate trace
elan lane lean lena neal
evil levi live veil vile
但是当我使用方法之后和之前的方法时sorted()filter()forEach()
words.stream()
.filter(s1 -> !alreadyFound.contains(s1) && words.stream()
.filter(s2 -> isAnagram.apply(s1, s2))
.count() == maxAnagrams)
.sorted()
然后它给出了这些结果:
abel able bale bela elba
abel able bale bela elba
alger glare lager large regal
angel angle galen glean lange
angel angle galen glean lange
abel able bale bela elba
abel able bale bela elba
caret carte cater crate trace
caret carte cater crate trace
caret carte cater crate trace
caret carte cater crate trace
elan lane lean lena neal
abel able bale bela elba
evil levi live veil vile
angel angle galen glean lange
alger glare lager large regal
angel angle galen glean lange
alger glare lager large regal
elan lane lean lena neal
angel angle galen glean lange
alger glare lager large regal
elan lane lean lena neal
elan lane lean lena neal
evil levi live veil vile
evil levi live veil vile
elan lane lean lena neal
alger glare lager large regal
caret carte cater crate trace
evil levi live veil vile
evil levi live veil vile
似乎在第二种方法程序中复制结果并将已经找到的单词添加到输出中。我想知道为什么会发生这种情况?
喵喔喔
相关分类