我想为MySortedSet调用一个构造函数,该构造函数将Comparator c作为参数。我该如何修改呢?

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表达式来代替:

MySortedSet<E> :: new

=>
() -> new MySortedSet<E>(c)

关于java - 使用Lambda在Java流中使用参数调用构造函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26783788/

10-11 22:32
查看更多