Empty listing in Java 6

Java 7 provides a convenient method

Collections.emptyEnumeration()

But this is not available in Java 6.

Is there an empty enumeration class hidden elsewhere in the JDK, or do I need to flip it?

+5
source share
2 answers

You can just use

 Collections.enumeration(Collections.emptyList()); 
+14
source

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(); } } 
+5
source

Source: https://habr.com/ru/post/1213042/


All Articles