Enumeration adapters in SE 8

Dan Smith daniel.smith at oracle.com
Wed Aug 24 11:10:11 PDT 2011

I was pointed to some comments on core-libs about adapting Enumerations to for loops in SE 8.  (Sorry for the new thread -- I wasn't subscribed.)  It turns out lambdas + extension methods will make this very easy.

In the API:

interface Enumeration<E> extends Iterator<E> {
  boolean hasMoreElements();
  E nextElement();
  boolean hasNext() default #hasMoreElements;
  E next() default #nextElement;
  void remove() default { throw new UnsupportedOperationException(); }

Note that Iterable is a function type -- a thunk producing an Iterator -- and so we can express Iterables with lambdas and method references.  It's becoming clear that a for loop should provide a target type for these things, so that will probably be part of the SE 8 feature set.

With a method reference:

Hashtable<String,Object> h = …;
for (String s : h#keys) { … }

With a lambda:

for (String s : #{ -> path().to().enumeration() }) { ... }

My preference is to also support Iterators directly in for loops; this may or may not make it in:

for (String s : path().to().enumeration()) { ... }


More information about the lambda-dev mailing list