2014-11-06 43 views
12

我想调用MySortedSet的构造函数,它将Comparator c作为参数。我如何修改这个来做到这一点?使用lambda中的Java流中的参数调用构造函数

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)); 
} 

回答

20

如果要将其他捕获的值作为参数传递,则不能使用方法引用。你将不得不使用lambda表达式来代替:

MySortedSet<E> :: new 

=>

() -> new MySortedSet<E>(c) 
相关问题