< prev index next >

jdk/src/java.base/share/classes/java/util/Enumeration.java

Print this page




  95      *
  96      * <pre>{@code
  97      *     JarFile jarFile = ... ;
  98      *     jarFile.entries().asIterator().forEachRemaining(entry -> { ... });
  99      * }</pre>
 100      *
 101      * (Note that there is also a {@link java.util.jar.JarFile#stream
 102      * JarFile.stream()} method that returns a {@code Stream} of entries,
 103      * which may be more convenient in some cases.)
 104      *
 105      * @implSpec
 106      * The default implementation returns an {@code Iterator} whose
 107      * {@link Iterator#hasNext hasNext} method calls this Enumeration's
 108      * {@code hasMoreElements} method, whose {@link Iterator#next next}
 109      * method calls this Enumeration's {@code nextElement} method, and
 110      * whose {@link Iterator#remove remove} method throws
 111      * {@code UnsupportedOperationException}.
 112      *
 113      * @return an Iterator representing the remaining elements of this Enumeration
 114      *
 115      * @since 1.9
 116      */
 117     default Iterator<E> asIterator() {
 118         return new Iterator<>() {
 119             @Override public boolean hasNext() {
 120                 return hasMoreElements();
 121             }
 122             @Override public E next() {
 123                 return nextElement();
 124             }
 125         };
 126     }
 127 }


  95      *
  96      * <pre>{@code
  97      *     JarFile jarFile = ... ;
  98      *     jarFile.entries().asIterator().forEachRemaining(entry -> { ... });
  99      * }</pre>
 100      *
 101      * (Note that there is also a {@link java.util.jar.JarFile#stream
 102      * JarFile.stream()} method that returns a {@code Stream} of entries,
 103      * which may be more convenient in some cases.)
 104      *
 105      * @implSpec
 106      * The default implementation returns an {@code Iterator} whose
 107      * {@link Iterator#hasNext hasNext} method calls this Enumeration's
 108      * {@code hasMoreElements} method, whose {@link Iterator#next next}
 109      * method calls this Enumeration's {@code nextElement} method, and
 110      * whose {@link Iterator#remove remove} method throws
 111      * {@code UnsupportedOperationException}.
 112      *
 113      * @return an Iterator representing the remaining elements of this Enumeration
 114      *
 115      * @since 9
 116      */
 117     default Iterator<E> asIterator() {
 118         return new Iterator<>() {
 119             @Override public boolean hasNext() {
 120                 return hasMoreElements();
 121             }
 122             @Override public E next() {
 123                 return nextElement();
 124             }
 125         };
 126     }
 127 }
< prev index next >