8中使用Flatmap组成流

8中使用Flatmap组成流

本文介绍了在Java 8中使用Flatmap组成流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们考虑一下我的以下课程:

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;
   }
}

其中ijk具有已知范围:r_ir_jr_k.现在我想生成此范围内A的所有可能实例.我可以想出类似的东西:

where i, j, k have a known range: r_i, r_j, r_k. Now I want to to generateall 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())

首先,它太冗长了.有什么办法可以缩短它?特别是我找不到rangeStream上.其次,编译器无法确定返回类型的类型.它认为是List<Object>而不是预期的List<A>.我该如何解决?

First, it's too verbose. Is there a way to shorten it? In particular I couldn't find a rangeon Stream. Second, the compiler cannot determine the type of the returned type. It considers it asList<Object> instead of the expected List<A>. How can I fix that?

推荐答案

使用range的一种方法是在之后立即执行拳击转换:

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());

这不是最漂亮的代码,但IntStream.range(0, max).boxed()仍然比Stream.iterate(0, n -> n+1).limit(max)更好……

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());

我看到的主要缺点是缺少IntPairTuple<int,int>类型.因此,它使用数组作为解决方法.

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.

这篇关于在Java 8中使用Flatmap组成流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 02:30