Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composing streams with flatmap in Java 8

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?

like image 823
Wickoo Avatar asked Mar 18 '23 10:03

Wickoo


1 Answers

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.

like image 145
Holger Avatar answered Mar 28 '23 08:03

Holger