4

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?

Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
Wickoo
  • 6,745
  • 5
  • 32
  • 45

2 Answers2

6

One way of using range is to perform a boxing conversion right afterwards:

List<A> list=IntStream.range(0, r_i).boxed()
  .flatMap(i -> IntStream.range(0, r_j).boxed()
    .flatMap(j -> IntStream.range(0, r_k)
      .mapToObj(k -> new A(i, j, k)))).collect(Collectors.toList());

It’s not the most beautiful code but IntStream.range(0, max).boxed() is still better than Stream.iterate(0, n -> n+1).limit(max)


One alternative is to use a real flattening operation rather than nested operations:

List<A> list=IntStream.range(0, r_i).boxed()
  .flatMap(i  -> IntStream.range(0, r_j).mapToObj(j -> new int[]{i,j}))
  .flatMap(ij -> IntStream.range(0, r_k).mapToObj(k -> new A(ij[0], ij[1], k)))
  .collect(Collectors.toList());

The main drawback I see is that it suffers from the absence of an IntPair or Tuple<int,int> type. So it uses an array as a work-around.

Holger
  • 285,553
  • 42
  • 434
  • 765
1

if it's ok to have mutable variable, you could get the List of class A like this....

List<A> newCollect = new ArrayList<>();
IntStream.range(0, r_i).forEach(
    i -> IntStream.range(0, r_j).forEach(
        j -> IntStream.range(0, r_k).forEach(
            k -> newCollect.add(new A(i, j, k))
        )
    )
);

or you can make List of List of List of A, then flatMap twice like this...

List<A> newCollect2 = IntStream.range(0, r_i).mapToObj(
    i -> IntStream.range(0, r_j).mapToObj(
        j -> IntStream.range(0, r_k).mapToObj(
            k -> new A(i, j, k)
        ).collect(Collectors.toList())
    ).collect(Collectors.toList())
)
.flatMap(a -> a.stream())
.flatMap(a -> a.stream())
.collect(Collectors.toList());
hanjoonk
  • 163
  • 1
  • 1
  • 13