the blog for developers

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.

You can leave a Reply here. Of course, you should follow me on twitter here.

You can share this post!
Do you want to tell others about this article? Use the social bookmark icons to submit this artice to the service of your choice. Thanks.

About the author: Stephan Schmidt is head of development at brands4friends. He has more than 15 years of internet technology experience and 10 years experience in agile. He was head of development, consultant and CTO and is a speaker, author and blog writer. He specializes in organizing and optimizing software development helping companies by increasing productivity with lean software development and agile methodologies. Want to know more? All views are only his own.
Leave a reply.

Comments

Stephan,

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

Michael E.

stephan

Thanks
-stephan

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.

Octavian NITA

Incredibly simple and useful!

Thanks a lot!

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/

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).

[...] Update: I somehow managed to miss Stephan’s recent post and Binkley’s older post. [...]

Octavian NITA

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

Thanks

Octavian NITA

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

Leave a Reply

What people wrote somewhere else:

Additional comments powered by BackType

Guide to CodeMonkeyism

Over the last 4 years I wrote many articles on this blog. To make it easier for you to find the relevant ones, I've organized them into topics.

Top 10

6 reasons why my VC funded startup did fail

Go Ahead: Next Generation Java Programming Style

Java Interview questions: Write a String Reverser

The dark side of NoSQL

7 Bad Signs not to Work for a Software Company or Startup

Is Java dead?

Scala vs. Clojure

Never, never, never use String in Java

No future for functional programming in 2008 – Scala, F# and Nu

Clojure vs Scala, Part 2

Java Developer

Is Java Dead?

Go Ahead: Next Generation Java Programming Style

Be careful with magical code

All variables in Java must be final

Never, never, never use String in Java

Bending Java: More readable code with methods that do nothing?

NoSQL Guy

NoSQL: The Dawn of Polyglot Persistence

The dark side of NoSQL

Essential storage tradeoff: Simple Reads vs. Simple Writes

Sharding destroys the goals of your relational database

The unholy legacy of databases

Startup/CTO

Development Dream Teams

6 reasons why my VC funded startup did fail

American vs. European style of Software Development

12 Things to Reduce Your Lead Time and Time to Market

The high cost of overhead when working in parallel

Essential storage tradeoff: Simple Reads vs. Simple Writes

Job Seeker

Another Good (Java) Interview Question

7 Bad Signs not to Work for a Software Company or Startup

Java Interview questions: Write a String Reverser (and use Recursion!)

Java Interview questions: Multiple Inheritance

As a Manager: What I value in developers

Top 10 Tips (+1) to Get a Pay Raise

Agilist

What Developers Need to Know About Agile

5 Practices Better to Change in Your Scrum Implementation

Scrum is not about engineering practices

ScrumMaster and ZenMaster: The joke of certification

What is Trans-Scrum?