我有一个列表和另一个数组列表。我想区分它们中的每一个并返回新的 ArrayList。
String[] input = scn.nextLine().split("\\s");
ArrayList<String> result = Arrays.stream(input).filter(a -> a.length() > 10).distinct()
我想返回ArrayList。我对返回列表没有任何问题(Collectors.asList 等......)
问候
最佳答案
您应该通过collect
方法收集结果:
ArrayList<String> result = Arrays.stream(input)
.filter(a -> a.length() > 10)
.distinct()
.collect(Collectors.toCollection(ArrayList::new));
如果您不关心特定的 List
实现,最好使用 toList()
收集器:
List<String> result = Arrays.stream(input)
.filter(a -> a.length() > 10)
.distinct()
.collect(Collectors.toList());
关于java - 函数式编程(Streams)返回类型ArrayList java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33320422/