Enumeration adapters in SE 8

Colin Decker cgdecker at gmail.com
Wed Aug 24 14:58:24 PDT 2011


I've come across various scenarios. One example is
javax.swing.tree.DefaultMutableTreeNode. It has numerous methods that return
Enumerations, including:
- children
- depthFirstEnumeration
- breadthFirstEnumeration
- preorderEnumeration
- postorderEnumeration
- pathFromAncestorEnumeration

I've created a utility class with methods to return Iterables for each of
these types of Enumerations for convenience, but it would be much nicer to
just use a method reference.

Deque's descendingIterator() method is a more modern good candidate for
treating as an Iterable. See this question on StackOverflow:
http://stackoverflow.com/questions/3883131/idiomatic-way-to-use-for-each-loop-given-an-iterator

-- 
Colin


On Wed, Aug 24, 2011 at 5:45 PM, Ben Evans <benjamin.john.evans at gmail.com>wrote:

> Hi Dan,
>
> On Wed, Aug 24, 2011 at 7:10 PM, Dan Smith <daniel.smith at oracle.com>
> wrote:
>
> > 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) { … }
> >
>
> I'm struggling to see where this might be useful. This example is obviously
> too trivial and would add nothing, but I can't see to come up with any more
> extensive example where having a method reference in this position would be
> actually useful.
>
> Can someone else provide a more compelling example for a for loop taking a
> method reference?
>
> Thanks,
>
> Ben
>
>


More information about the lambda-dev mailing list