有什么办法可以将这些嵌套的IntStream合并为一个吗?有更短的方法吗? [英] Is there any way to flatten these nested IntStreams into one? Is there shorter way?

查看:62
本文介绍了有什么办法可以将这些嵌套的IntStream合并为一个吗?有更短的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将4个嵌套for循环更改为嵌套IntStream,但是此解决方案看起来不太好.我不知道如何缩短时间?我应该以某种方式使用flatmap吗?

I changed 4 nested for loops to nested IntStream but this solution doesn't look good. I don't know how to make it shorter? Should I use flatmap in some way?

IntStream.range(0, totalCluster).forEach(numCluster ->{
  writeToFile("Cluster__________________________" + numCluster);
  IntStream.range(0, totalAgency).forEach(numAgency ->{
    writeToFile("\n\tCluster_" + numCluster + "_Agency_" + numAgency);
    IntStream.range(0, totalProgramArea).forEach(numProgramArea ->IntStream.range(0, totalUsers).forEach(numUser ->{
      writeToFile("\n\t\t\tAgency_" + numAgency + "_" + "ProgramArea_" + numProgramArea + "_User_" + numUser);
    }));
  });
});

推荐答案

您可能正在问有关形式的构造

You are probably asking about a construct in form

IntStream.range(0, 2)
  .flatMap(i -> IntStream.range(0, 2))
  .flatMap(i -> IntStream.range(0, 2))
  .forEach(i -> /*... inner loop logic here ..*/ );

但是,如果您需要最内层逻辑中每个外循环的每个迭代的索引,则没有很好的方法.您的问题的答案是-老式的循环在这里效果更好.

However, if you need the index of each iteration of every outer loop inside the innermost logic, there is no nice way of doing it. The answer to your question is - old fashioned for loops work better here.

仍然,这是一个示例(我减少了混乱以提高可读性):

Still, here is one example (I reduced clutter to improve readability):

IntStream.range(0, totalClusters).boxed()
  .flatMap(i -> IntStream.range(0, totalAgencies).mapToObj(j -> new int[]{i,j})). 
  .flatMap(k -> IntStream.range(0, totalAreas).mapToObj(j -> new int[]{k[0],k[1],j}))
  .forEach(o -> System.out.println(Arrays.toString(o)));

它打印

[0, 0, 0]
[0, 0, 1]
...
[1, 1, 1]

此代码的问题是您必须在堆上分配int数组,而不是使用堆栈中的循环计数器.我只使用int[]为简单起见,这不是一个好习惯,实际上最好使用一些上下文对象.

The problem with this code is that you have to allocate int arrays on heap instead of using loop counters from stack. I only used int[] for simplicity, it is not a good practice, in reality it is better to use some context object.

您可以从这里获得解决方案的想法.

You can derive an idea of a solution from here.

现在,人们经常问是否有适当的功能来处理嵌套的for循环.在类似Haskell的语言中,您将使用类似这样的内容,因为列表是单子(或列表推导):

Now, people often ask if there is a proper functional way of dealing with nested for loops. In a language like Haskell, you would use something like this, because lists are monads (or list comprehensions):

do
  i <- [0..2]
  j <- [0..3]
  return $ i*100 + j

通过创建自己的函数组合器库,您绝对可以在Java中使用类似的do-notation逻辑.在这种特殊情况下,即使有可能,与Scala不同,Java的语法也可以防止最终结果看起来比老式的for循环更好.

You can definitely go for similar do-notation logic in Java, by creating your own library of function combinators. Even though it is possible, unlike in Scala, syntax of Java prevents the end result from looking better than old fashioned for loops, in this particular case.

这篇关于有什么办法可以将这些嵌套的IntStream合并为一个吗?有更短的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆