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?
Of course you can!
The java. util. EnumSet. noneOf(Class<E> elementType) method creates an empty enum set with the specified element type.
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.
You can simply use
Collections.enumeration(Collections.emptyList());
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(); }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With