Java:遍历列表的列表?

发布于 2024-09-01 02:27:33 字数 844 浏览 5 评论 0 原文

问题但是在C#中。那么Java有C#的命令吗?我需要它来匹配-SearchTerm-文件关系。

foreach(var i in BunchOfItems.SelectMany(k => k.Items)) {}

[为什么不使用 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;
}

The question but in C#. So does Java have C#'s command? I need it for Matches-SearchTerm-Files-relationship.

foreach(var i in BunchOfItems.SelectMany(k => k.Items)) {}

[Why not for-loops?]
I have done such structures in nested for loops but they soon become bloated. So I prefer something more succint like the above.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

如果没有 2024-09-08 02:27:33
for (List<Object> lo : list) {
    for (Object o : lo) {
        // etc etc
    }
}

我认为没有更简单的解决方案。

for (List<Object> lo : list) {
    for (Object o : lo) {
        // etc etc
    }
}

I don't think there's a simpler solution.

丑疤怪 2024-09-08 02:27:33

如果您可以将数据放入 Iterable> 中,那么您可以使用 Guava 的 Iterables 将其转换为扁平化的 Iterable .concat 方法。如果您拥有的确实是一个 Iterable,并且可以通过某种方式从 S 转换为 Iterable,那么,那么你必须首先使用 Iterables.transform 将其视为 concat 所需的 Iterable>

如果 Java 有类似闭包的东西,所有这一切看起来都会好得多,但至少在今天这是可能的。

http://guava-libraries.googlecode.com

If you can get the data into an Iterable<Iterable<T>>, then you can get from that to a flattened Iterable<T> using Guava's Iterables.concat method. If what you have is really an Iterable<S>, with some way to get from an S to an Iterable<T>, well, then you have to first use Iterables.transform to view that as the Iterable<Iterable<T>> needed by concat.

All this will look a lot nicer if and when Java has something resembling closures, but at least today it's possible.

http://guava-libraries.googlecode.com

对于 Java 8,您可以说

Collection bunchOfItems = ...;
bunchOfItems.stream().flatMap(k::getItems).forEach(i -> /* operate on i */);

或 ,

Item[] bunchOfItems = ...;
Stream.of(bunchOfItems).flatMap(k::getItems).forEach(i -> /* operate on i */);

具体取决于您是否拥有 Collection 还是 Array

With Java 8, you can say

Collection bunchOfItems = ...;
bunchOfItems.stream().flatMap(k::getItems).forEach(i -> /* operate on i */);

or

Item[] bunchOfItems = ...;
Stream.of(bunchOfItems).flatMap(k::getItems).forEach(i -> /* operate on i */);

depending upon whether you have a Collection or an Array.

拥抱影子 2024-09-08 02:27:33

请耐心等待大约半年的时间,直到 JDK7 最终版本,其中将包含闭包。这提供了与 LINQ 类似的语法和相同的可能性,这在您所讨论的答案中得到了演示。

Have about half a year patience until JDK7 is final which will include Closures. This provides simliar syntax and the same possibilities as LINQ which was demonstrated in the answer you're talking about.

记忆消瘦 2024-09-08 02:27:33

我有自己的版本。绝望地等待 Java 中的闭包

public static <T, E> Iterable<T> transformMany(Iterable<E> iterable, Func<E, Iterable<T>> f) {
    if (null == iterable)
        throw new IllegalArgumentException("null iterable");
    if (null == f)
        throw new IllegalArgumentException("null f");

    return new TransformManyIterable<E, T>(iterable, f);
}

public interface Func<E, T> {
    T execute(E e);
}

public class TransformManyIterable<TOriginal, TResult> implements Iterable<TResult> {
    private Iterable<TOriginal> iterable;
    private Func<TOriginal, Iterable<TResult>> func;

    public TransformManyIterable(Iterable<TOriginal> iterable,
            Func<TOriginal, Iterable<TResult>> func) {
        super();
        this.iterable = iterable;
        this.func = func;
    }

    class TransformIterator implements Iterator<TResult> {
        private Iterator<TOriginal> iterator;
        private Iterator<TResult> currentIterator;

        public TransformIterator() {
            iterator = iterable.iterator();
        }

        @Override
        public boolean hasNext() {
            if (currentIterator != null && currentIterator.hasNext())
                return true;
            else {
                while (iterator.hasNext()) {
                    Iterable<TResult> iterable = func.execute(iterator.next());
                    if (iterable == null)
                        continue;
                    currentIterator = iterable.iterator();
                    if (currentIterator.hasNext())
                        return true;
                }
            }

            return false;
        }

        @Override
        public TResult next() {
            if (currentIterator != null && currentIterator.hasNext())
                return currentIterator.next();
            else {
                while (iterator.hasNext()) {
                    Iterable<TResult> iterable = func.execute(iterator.next());
                    if (iterable == null)
                        continue;
                    currentIterator = iterable.iterator();
                    if (currentIterator.hasNext())
                        return currentIterator.next();
                }
            }
            throw new NoSuchElementException();
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    }

    @Override
    public Iterator<TResult> iterator() {
        return new TransformIterator();
    }

}

用法:

    Iterable<SomeType> result = transformMany(input, new Func<InputType, Iterable<SomeType>>() {
        @Override
        public Iterable<SomeType> execute(InputType e) {
            return new ArrayList<SomeType>();
        }
    });

I have my own version. Waiting desperately for Closures in Java :

public static <T, E> Iterable<T> transformMany(Iterable<E> iterable, Func<E, Iterable<T>> f) {
    if (null == iterable)
        throw new IllegalArgumentException("null iterable");
    if (null == f)
        throw new IllegalArgumentException("null f");

    return new TransformManyIterable<E, T>(iterable, f);
}

public interface Func<E, T> {
    T execute(E e);
}

public class TransformManyIterable<TOriginal, TResult> implements Iterable<TResult> {
    private Iterable<TOriginal> iterable;
    private Func<TOriginal, Iterable<TResult>> func;

    public TransformManyIterable(Iterable<TOriginal> iterable,
            Func<TOriginal, Iterable<TResult>> func) {
        super();
        this.iterable = iterable;
        this.func = func;
    }

    class TransformIterator implements Iterator<TResult> {
        private Iterator<TOriginal> iterator;
        private Iterator<TResult> currentIterator;

        public TransformIterator() {
            iterator = iterable.iterator();
        }

        @Override
        public boolean hasNext() {
            if (currentIterator != null && currentIterator.hasNext())
                return true;
            else {
                while (iterator.hasNext()) {
                    Iterable<TResult> iterable = func.execute(iterator.next());
                    if (iterable == null)
                        continue;
                    currentIterator = iterable.iterator();
                    if (currentIterator.hasNext())
                        return true;
                }
            }

            return false;
        }

        @Override
        public TResult next() {
            if (currentIterator != null && currentIterator.hasNext())
                return currentIterator.next();
            else {
                while (iterator.hasNext()) {
                    Iterable<TResult> iterable = func.execute(iterator.next());
                    if (iterable == null)
                        continue;
                    currentIterator = iterable.iterator();
                    if (currentIterator.hasNext())
                        return currentIterator.next();
                }
            }
            throw new NoSuchElementException();
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    }

    @Override
    public Iterator<TResult> iterator() {
        return new TransformIterator();
    }

}

Usage:

    Iterable<SomeType> result = transformMany(input, new Func<InputType, Iterable<SomeType>>() {
        @Override
        public Iterable<SomeType> execute(InputType e) {
            return new ArrayList<SomeType>();
        }
    });
陌若浮生 2024-09-08 02:27:33

SelectMany 方法是 .Net 特定的 LINQ 的一部分。 这个问题询问java的LINQ等效项。不幸的是,看起来并没有直接的等价物。

The SelectMany method is part of LINQ which is .Net-specific. This question asks about a LINQ equilvalent for java. Unfortunately, it doesn't look like there is a direct equivalent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文