java stream collect function giving error

Curious :

First statement works but not second giving below error, why ?

java.util.Arrays.asList(1,2,3,4,5).stream()
                                  .map(n -> n+1)
                                  .collect(Collectors.toList());

List<Integer> list = IntStream.rangeClosed(1, 10)
                              .map(n -> n + 1)
                              .collect(Collectors.toList());

ERROR:

Type mismatch: cannot convert from Collector<Object,capture#5-of ?,List<Object>> 
to Supplier<R>
Joe C :

While there is a collect method on a Stream which accepts a Collector, there is no such method on an IntStream.

You can convert your IntStream to a Stream<Integer> using the boxed() method.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=465325&siteId=1