Top community members
All Wiki Articles Create Wiki Article

Have you ever heard about encyclopedia for code?

What about: Wiki for Code project?

* because I've tried to share code and my solutions on different services and it was removed or I was unwelcomed
   - maybe it wasn't good place, but this one is.

if you think you have solution that can help someone, you are welcome to share this solution - Click here

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

0 contributions
5 points

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

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey šŸ‘‹
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more