Let's consider I have the following class:
class A {
int i, j, k;
public A(int i, int j, int k) {
this.i = i; this.j = j; this.k = k;
}
}
where i
, j
, k
have a known range: r_i
, r_j
, r_k
. Now I want to to generate
all possible instances of A
in this range. I could come up with something like:
Stream.iterate(0, n -> ++n).limit(r_i)
.flatMap(i -> Stream.iterate(0, n -> ++n).limit(r_j)
.flatMap(j -> Stream.iterate(0, n -> ++n).limit(r_k)
.map(k -> new A(i, j, k)))).collect(Collectors.toList())
First, it's too verbose. Is there a way to shorten it? In particular I couldn't find a range
on Stream
. Second, the compiler cannot determine the type of the returned type. It considers it as
List<Object>
instead of the expected List<A>
. How can I fix that?
IntStream.range
to start with. – BauskeIntStream.range
andmapToObj
at the end. Are you sure your compiler complains or is it just your IDE? I just copied your code in IntelliJ IDEA 14 and it does not show any warnings or errors or any sort. – LenniIntStream.range
, the problem is that there should be aflatMapToObj
or something. Can you try withmapToObj
only and show me the code? – LentissimoIntStream.range
, could you get that running? – Lentissimorange
? I'm curious. – Lentissimo