0

I would like to convert Stream of random numbers to list:

public static void main(String[] args) {
        System.out.println(
                new Random().ints(10, 0, 20).
                        collect(Collectors.toList()));
    }

But that gives an exception

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)

so how can I convert stream to list in java?

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
milanHrabos
  • 2,010
  • 3
  • 11
  • 45
  • The `ints()` method returns an `IntStream`, see https://stackoverflow.com/questions/23674624/how-do-i-convert-a-java-8-intstream-to-a-list – M A May 21 '21 at 08:23
  • 1
    Does this answer your question? [How do I convert a Java 8 IntStream to a List?](https://stackoverflow.com/questions/23674624/how-do-i-convert-a-java-8-intstream-to-a-list) – Gautham M May 21 '21 at 09:18

4 Answers4

6

Explanation

Random#ints returns IntStream, not a Stream<Integer>, that is the problem.

IntStream is a special class to represent primitive ints. But generics in Java do not support primitives, i.e. there is no List<int>. So you first have to make your ints Integers, the wrapper class. Then you can collect to List<Integer>.


Solution

Just box it and it works.

stream.boxed().toList() // since Java 16
// or
stream.boxed().collect(Collectors.toList())

Also see the documentation of IntStream#boxed:

Returns a Stream consisting of the elements of this stream, each boxed to an Integer.

For understanding, boxed() is roughly equivalent to

stream.mapToObj(x -> Integer.valueOf(x))

So in your case:

public static void main(String[] args) {
    System.out.println(
        new Random()
            .ints(10, 0, 20)
            .boxed()
            .toList());
}
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
3

A List cannot contain primitives, so you need to use a wrapper type such as Integer. To achieve this, call boxed() (which converts your IntStream to a Stream<Integer>) prior to calling collect().

public static void main(String[] args) {
        System.out.println(
                new Random().ints(10, 0, 20).boxed().
                        collect(Collectors.toList()));
}
Jeroen Steenbeeke
  • 3,884
  • 5
  • 17
  • 26
2

You need to call boxed in order to get Stream<Integer> from IntStream and access the desired collect overloaded method:

new Random().ints(10, 0, 20)
            .boxed()
            .collect(Collectors.toList());
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
2

Just use .boxed() in your IntStream reference.

for eg:

import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Java8 {
    public static void main(String[] args) {
        IntStream ints = new Random().ints(10, 0, 20); //here ints is your intstream
        List<Integer> collect = ints.boxed().collect(Collectors.toList()); 
/*
  Here Just use ints.boxed() and then collect to list 
*/
        collect.forEach(System.out::println);
    }
}
Sundar Gautam
  • 445
  • 1
  • 6
  • 10