我正在尝试对名为 parts
的 ArrayList 执行以下流操作:
parts
.stream()
.map(t -> t.toLowerCase())
.distinct()
.sorted()
.collect(Collectors.toList());
其中 parts
包含这样的字符串:
Adventures
in
Disneyland
Two
blondes
were
going
to
Disneyland
....
除了查看调试器外,部分
根本没有改变。不确定我是否遗漏了流程的某些步骤?
最佳答案
upon looking at the debugger, "parts" isn't changed at all
正确。流不会修改用于创建流的集合的元素。流最终返回一个包含映射项目的新 List
。只需创建一个变量并将流操作的返回值分配给它:
List<String> modifiedList = parts.stream()
.map(t -> t.toLowerCase())
.distinct()
.sorted()
.collect(Collectors.toList());
关于Java Streams 方法不影响我的 ArrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54916333/