Languages
[Edit]
EN

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

5 points
Created by:
AnnLen
1771

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

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.