Java 6中的空枚举

hen*_*nry 6 java

Java 7提供了方便的方法

Collections.emptyEnumeration()

但这在Java 6中不可用.

是否有一个空的枚举类潜伏在JDK的其他地方,或者我需要自己滚动吗?

JB *_*zet 18

你可以简单地使用

Collections.enumeration(Collections.emptyList());
Run Code Online (Sandbox Code Playgroud)


use*_*476 5

JDK 6中没有空的Enumeration,但您可以使用jdk 7中的源代码

    /*
     * taken from jdk source
     * @since 1.7
     */
    public static <T> Enumeration<T> emptyEnumeration() {
        return (Enumeration<T>) EmptyEnumeration.EMPTY_ENUMERATION;
    }

    private static class EmptyEnumeration<E> implements Enumeration<E> {
        static final EmptyEnumeration<Object> EMPTY_ENUMERATION
            = new EmptyEnumeration<>();

        public boolean hasMoreElements() { return false; }
        public E nextElement() { throw new NoSuchElementException(); }
    }
Run Code Online (Sandbox Code Playgroud)