Use Java 5 for with an Enumeration

Reading the post Searching Jars Redux on the “Code To Joy” blog, where Michael shows a closure Example in Java iterating through Jar content, saying ” old-style Enumeration, don’t blame me or closures!” about some ugly code using an Enumeration. It would be nice to just use for to iterate over an enumeration. This can be done easily. You can use Enumerations with the Java 5 for syntax when using a little Adapter around an Enumeration.

  public static void main(String[] args) {
    Vector<String> vector = new Vector<String>();
    vector.add("Stephan");
    for (String name : iterate(vector.elements())) {
      System.out.println("Name: " + name);
    }
  }

  public static <T> Iterable<T> iterate(Enumeration<T> enumeration) {
    return new IterableEnumeration<T>(enumeration);
  }

The IterableEnumeration is quite easy to write:

public class IterableEnumeration<T> implements Iterable<T>, Iterator<T> {
    private Enumeration<T> enumeration;

    public IterableEnumeration(Enumeration<T> enumeration) {
      this.enumeration = enumeration;
    }

    public Iterator<T> iterator() {
      return this;
    }

    public boolean hasNext() {
      return enumeration.hasMoreElements();
    }

    public T next() {
      return enumeration.nextElement();
    }

    public void remove() {
      throw new UnsupportedOperationException();
    }
  }

Together with static imports and a static iterate method, the Java code looks much nicer.

Go for more beautiful Java.

Thanks for listening.


10 Responses to “Use Java 5 for with an Enumeration”  

  1. Gravatar Icon 1 Michael Easter

    Stephan,

    Nice job! I hadn’t considered a way to bring Enumeration into Java 5

    Michael E.

  2. Gravatar Icon 2 stephan

    Thanks
    -stephan

  3. Gravatar Icon 3 B. K. Oxley (binkley)
  4. Gravatar Icon 4 stephan

    Ah yes, as I said it’s easy to do. Would be nice if Java supported Adapter/Delegation classes with some nice syntactic sugar.

    At least static imports shorten the code to use the Adapter.

  5. Gravatar Icon 5 Octavian NITA

    Incredibly simple and useful!

    Thanks a lot!

  6. Gravatar Icon 6 Dr. Graversen

    What is wrong with Collections.list() it’s been around since jdk 1.4

    ArrayList list(Enumeration e)

    Returns an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration.

    http://supercsv.sourceforge.net/

  7. Gravatar Icon 7 stephan

    I guess Collections.list() returns a new List not a wrapper around the Enumeration. So this will create a new object and a.) the Enumeration is iterated (which might be expensiv, think of 1 billion entries, while you only want the first 10) b.) memory for the entries is created (think of 1 billioen entries again, while you only want the first 10).

  8. Gravatar Icon 8 Octavian NITA

    See some enhancement at http://tavi-mytechblog.blogspot.com/… eventually let me know what you think…

    Thanks

  9. Gravatar Icon 9 Octavian NITA

    Sorry, the address is: http://tavi-mytechblog.blogspot.com/

  1. 1 Javablog » Enumeration and Iterable


Leave a Reply



RSS