Languages
[Edit]
EN

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

5 points
Created by:
AnnLen
9180

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

Native Advertising
50 000 ad impressions - 449$
šŸš€
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

ā¤ļøšŸ’» šŸ™‚

Join