在 for-each 语句中使用枚举而没有 RAM 限制?

发布于 2024-10-16 21:34:17 字数 135 浏览 1 评论 0原文

您好,我有大约 1000 万个值,并在增强 for 循环中进行枚举,但它耗尽了我的 RAM。有没有办法获得迭代而不是枚举。

我试图找到 Collections.list() 和 Collections.enumeration() 的替代品。

Hi I have about 10 million values and getting Enumeration in enhanced-for loop, but It blasts my RAM. Is there any way to get Iteration rather than Enumeration.

I am trying to to find an alternate for Collections.list() and Collections.enumeration().

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

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

发布评论

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

评论(2

千秋岁 2024-10-23 21:34:17
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
public final class Enumerations {

    /**
     * Allows using of {@link Enumeration} with the for-each statement. The
     * implementation is not using any heap space and such is able to serve
     * virtually endless Enumerations, while {@link Collections#list} is limited
     * by available RAM. As a result, this implementation is much faster than
     * Collections.list.
     * 
     * @param enumeration
     *            The original enumeration.
     * @return An {@link Iterable} directly calling the original Enumeration.
     */
    public static final <T> Iterable<T> iterable(final Enumeration<T> enumeration) {
        return new Iterable<T>() {
            public final Iterator<T> iterator() {
                return new Iterator<T>() {
                    public final boolean hasNext() {
                        return enumeration.hasMoreElements();
                    }

                    public final T next() {
                        return enumeration.nextElement();
                    }

                    /**
                     * This method is not implemeted as it is impossible to
                     * remove something from an Enumeration.
                     * 
                     * @throws UnsupportedOperationException
                     *             always.
                     */
                    public final void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
        };
    }

}
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
public final class Enumerations {

    /**
     * Allows using of {@link Enumeration} with the for-each statement. The
     * implementation is not using any heap space and such is able to serve
     * virtually endless Enumerations, while {@link Collections#list} is limited
     * by available RAM. As a result, this implementation is much faster than
     * Collections.list.
     * 
     * @param enumeration
     *            The original enumeration.
     * @return An {@link Iterable} directly calling the original Enumeration.
     */
    public static final <T> Iterable<T> iterable(final Enumeration<T> enumeration) {
        return new Iterable<T>() {
            public final Iterator<T> iterator() {
                return new Iterator<T>() {
                    public final boolean hasNext() {
                        return enumeration.hasMoreElements();
                    }

                    public final T next() {
                        return enumeration.nextElement();
                    }

                    /**
                     * This method is not implemeted as it is impossible to
                     * remove something from an Enumeration.
                     * 
                     * @throws UnsupportedOperationException
                     *             always.
                     */
                    public final void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
        };
    }

}
山色无中 2024-10-23 21:34:17

我经常使用的一个可能有帮助的技巧是,你有一个需要集合的方法。

 populate(List<String> list);

并且您不想更改该方法,但您知道它只使用 add() 方法。您可以执行以下操作

 List<String> list = new ArraysList<String>() {
    public boolean add(String text) {
        myProcess(text);
        return false;
    }
 };
 populate(List<String> list);

在这种情况下,populate 可以添加任意数量的数据,而无需使用额外的内存。

A trick I use quite often which may help is say you have a method which takes a collection.

 populate(List<String> list);

and you don't want to change the method but you know it only uses the add() method. You can do the following

 List<String> list = new ArraysList<String>() {
    public boolean add(String text) {
        myProcess(text);
        return false;
    }
 };
 populate(List<String> list);

In this case, populate can add any amount of data without using additional memory.

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