Languages
[Edit]
EN

Java 8 - convert Int Stream to list with Collectors - error fix

5 points
Created by:
maxsior322
347

FIX for error when converting Int Stream to Collectors

Single method call .boxed() on IntStream API can fix this problem.

Example:

IntStream.of(1, 2, 3, 4, 5)
         .boxed()                       // <-- this line fix the problem
         .collect(Collectors.toList());

Full working example

Below we have 2 parts of the code. First part is error reproduction. Second part shows the fix, when we want to convert int stream to list with collect method using int stream and calling boxed method.

// ERROR reproduction:
// Error:(18, 17) java: method collect in interface
// java.util.stream.IntStream cannot be applied to given types;
// ...
// IntStream.of(1, 2, 3, 4, 5)
//          .collect(Collectors.toList());


// FIX:
List<Integer> collect = IntStream.of(1, 2, 3, 4, 5)
                                 .boxed()          // <-- this line fix the problem
                                 .collect(Collectors.toList());

// [1, 2, 3, 4, 5]
System.out.println(collect);

Full stack trace from intellij idea:

Error:(18, 17) java: method collect in interface java.util.stream.IntStream 
cannot be applied to given types;
  required: java.util.function.Supplier<R>,
  java.util.function.ObjIntConsumer<R>,java.util.function.BiConsumer<R,R>
  found: java.util.stream.Collector<java.lang.Object,capture#1 
  of ?,java.util.List<java.lang.Object>>
  reason: cannot infer type-variable(s) R
    (actual and formal argument lists differ in length)

Screenshot with this error: 

Intellij idea Error: IntStream cannot be applied to given types screenshot
Intellij idea Error: IntStream cannot be applied to given types - screenshot

Fix when using Random().ints()

It is very common problem when we want to collect int stream given from Random class and we call .collect(Collectors.toList()) directly on .ints(size, min, max) givining us error java.util.stream.IntStream cannot be applied to given types. The solution is the same as above, we need to call .boxed() method on IntStream API.

Quick fix:

new Random()
        .ints(5, 1, 10)
        .boxed()                          // <-- this line fix the problem
        .collect(Collectors.toList());

Code example with error reproduction and simple fix.

int size = 5;
int min = 1;
int max = 10;

// ERROR:
// Error:(26, 67) java: method collect in interface
// java.util.stream.IntStream cannot be applied to given types;
// ...
// List<Integer> list = new Random()
//                          .ints(size, min, max)
//                          .collect(Collectors.toList());


// FIX:
List<Integer> list = new Random()
                          .ints(size, min, max)
                          .boxed()            // <-- this line fix the problem
                          .collect(Collectors.toList());

// [5, 9, 4, 3, 4]
System.out.println(list);

References

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join