使用Lambda在Java流中使用参数调用构造函数 [英] Call constructor with parameter inside Java stream with lambda
本文介绍了使用Lambda在Java流中使用参数调用构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想调用MySortedSet的构造函数,该构造函数将Comparator c作为参数.我该如何修改呢?
I want to call a constructor for MySortedSet that takes in a Comparator c as a parameter. How can I modify this to do so?
public MySortedSet<E> subSet(E fromElement, E toElement) {
return list.stream()
.filter(x -> (list.indexOf(x) <= list.indexOf(fromElement)
&& list.indexOf(x) < list.indexOf(toElement)))
.collect(Collectors.toCollection(MySortedSet<E> :: new));
}
推荐答案
如果要传递其他捕获的值作为参数,则不能使用方法引用.您将不得不使用lambda表达式代替:
You can’t use method references if you want to pass additional captured values as parameters. You will have to use a lambda expression instead:
MySortedSet<E> :: new
=>
() -> new MySortedSet<E>(c)
这篇关于使用Lambda在Java流中使用参数调用构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文