Java:迭代列表列表?

hhh*_*hhh 6 java iteration list

问题,但在C#.那么Java有C#的命令吗?我需要它用于Matches-SearchTerm-Files-relationship.

foreach(var i in BunchOfItems.SelectMany(k => k.Items)) {}
Run Code Online (Sandbox Code Playgroud)

[为什么不for循环?] 我已经在嵌套for循环中完成了这样的结构,但它们很快变得臃肿.所以我更喜欢像上面这样的更多傻瓜.

public static Stack<Integer[]> getPrintPoss(String s,File f,Integer maxViewPerF)
{
    Stack<File> possPrint = new Stack<File>();
    Integer[] poss = new Integer[4]();
    int u,size;
    for(File f:files)
    { 
        size = f2S(f).length();
        u = Math.min(maxViewsPerF,size);
        for(int i=0; i<u;i++)
        {
           // Do something --- bloated, and soon out of control
           // wants more succintly

        }
    }
    return possPrint;
}
Run Code Online (Sandbox Code Playgroud)

som*_*guy 7

for (List<Object> lo : list) {
    for (Object o : lo) {
        // etc etc
    }
}
Run Code Online (Sandbox Code Playgroud)

我不认为有一个更简单的解决方案.


Kev*_*ion 7

如果你可以将数据转换成a Iterable<Iterable<T>>,那么你可以Iterable<T>使用Guava的Iterables.concat方法从中获得数据.如果你拥有的东西真的是一个Iterable<S>,有一些方法可以从一个S到一个Iterable<T>,那么,那么你必须首先使用它Iterables.transform来查看Iterable<Iterable<T>>需要的东西concat.

如果Java有类似闭包的东西,所有这些看起来会更好,但至少在今天它是可能的.

http://guava-libraries.googlecode.com


Bal*_*usC 1

大约需要半年的耐心,直到 JDK7 完成,其中将包含闭包。这提供了与 LINQ 类似的语法和相同的可能性,这在您所讨论的答案中得到了演示。