Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty Enumeration in Java 6

Tags:

java

Java 7 provides the convenience method

Collections.emptyEnumeration()

But this is not available in Java 6.

Is there an empty enumeration class lurking elsewhere in the JDK, or do I need to roll my own?

like image 218
henry Avatar asked Feb 10 '15 11:02

henry


People also ask

Can you have an empty enum in Java?

Of course you can!

How do you create an empty enum in Java?

The java. util. EnumSet. noneOf(Class<E> elementType) method creates an empty enum set with the specified element type.

What is enumeration method in Java?

An enumeration (enum for short) in Java is a special data type which contains a set of predefined constants. You'll usually use an enum when dealing with values that aren't required to change, like days of the week, seasons of the year, colors, and so on.


2 Answers

You can simply use

Collections.enumeration(Collections.emptyList());
like image 76
JB Nizet Avatar answered Oct 04 '22 05:10

JB Nizet


there is no empty Enumeration in JDK 6, but you can use the source code from 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(); }
    }
like image 22
user2088476 Avatar answered Oct 04 '22 03:10

user2088476