Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call constructor with parameter inside Java stream with lambda

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));
}
like image 530
Jessica Avatar asked Nov 06 '14 15:11

Jessica


1 Answers

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)
like image 181
Holger Avatar answered Oct 06 '22 08:10

Holger