Java for .net-guys or foreach in Java

Tags:
Add comments

Disclaimer: I’m absolutely not the person who can tell a lot about Java. I learned it a few years ago for university, but used it most of the time only for small example stuff. Never as deep as I’m working with .net. But maybe my newest discoveries are useful for more programmers with .net background.

I started using Java 1.4 and until last week I never took a look at Java 5 or 6. The only thing I was aware of were generics in Java.

iterators in Java

I was always annoyed in the missing of a foreach loop in Java. There is a concept of iterators in Java comparable to the .net Enumerators. But to loop over a list you will find in millions of Java programs the following code:

Iterator<Integer> i = list.iterator();
while(i.hasNext()) {
    Integer current = i.next();

    // do something with the current element
}

Sometimes build into a regular for loop, but I don’t think that is more readable or convenient:

for(Iterator<Integer> i = list.iterator(); i.hasNext(); ) {
    Integer current = i.next();

    // just do something
}

By the way: Can anyone tell me why the method is called iterator() and not getIterator() as used everywhere else in Java?

foreach in Java

With Java 5 (or 1.5 – call it as you like) a foreach loop was introduced. This is about five years ago, so I’m very fast in getting news from the Java world.

for(Integer i : list) {
    // just use i as the current element
}

I like it. It’s working for everything that implements java.lang.Iterable and for regular arrays.

3 Responses to “Java for .net-guys or foreach in Java”

  1. Arne Says:

    About the iterator() vs. getIterator() issue: as I understand it I’d say that getters are meant to be used for “fetching” properties of an object (for JavaBeans its even specified this way in the JavaBeans spec), i.e. getPort() on a java.net.URL returns its port as an integer. Iterators in contrast are not a property, but a different interface to access a collection, thus you can’t just fetch “the iterator” of a collection, there’s some code involved in creating it, and there might by more than one for each collection. The size of a collection (returned by size()) is a similar case, since the size is derived from the contents of a collection.
    But from my experience with code “out there” I’d say that this rule isn’t always followed very strictly.

  2. Benjamin Schröter Says:

    @Arne: That makes sense. In .net (and my mind) the convention is exactly the other way: Properties are .net constructs and do not need a prefix for getter and setters. But when you have to do a lot of work to provide some date you will call the method GetSomething() instead of using a Something-property.

  3. Kris Says:

    just stumbled upon this page as I was looking for a simpler way to do this (and also figure out the ‘getIterator’ vs ‘iterator’ business), and you’ve given a great way. Thanks! I’m a .NET guy normally so I’ll be giving your blog a good read!

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in