• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Of Enumerators and Iterators

 
Alex Birmingham
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all, just a few questions above the abovementioned topic:

1. Enumators/enumeration: I seem to be finding two very different definitions of this apparently same concept:

a) Enumeration is an interface much like Iteration, in that it can be used in order to parse the elements of a List, using methods such as enumerator.hasNext(), etc.

b) Enumeration, or Enum, is a tool which can be used for creating statics within a class. So for example if I want to create an inventory list for a product, and one instance variable of said product has only three or four possible values, then I may as well use an Enum of static-valued strings as opposed to just a regular old open-ended string.

2) Iterators and Enumeration, or at least my first (a) definition of enumeration: Aren't these interfaces? As in abstract class-like things which one implements, and can't instantiate? Why is it I always see these so-called 'interfaces' being instantiated within for()?
Quientessential example:
for(Iterator it = data.iterator(); itHasNext()

3) Why use iterators/enumerators over just a simple for (Object a: ObjectList) {} statement? It seems to accomplish the same thing, and is far more legible? Java Head First started out with the simple for() version, but has recently made the swap to iterators, and I'm not sure I understand why.

Thanks in advance!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alex Birmingham:
1. Enumators/enumeration: I seem to be finding two very different definitions of this apparently same concept:

a) Enumeration is an interface much like Iteration, in that it can be used in order to parse the elements of a List, using methods such as enumerator.hasNext(), etc.


Yes, java.util.Enumeration and java.util.Iterator serve exactly the same purpose. Iterator was introduced in JDK 1.2; it's intended as an "improved" Enumeration, which was present in Java 1.0. It's improved in several respects: the name is more like the standard comp-sci name for the concept, and the method names are shorter and thus more convenient. Otherwise it's the same. Use Iterator when you can, Enumeration only when you have to.


b) Enumeration, or Enum, is a tool which can be used for creating statics within a class. So for example if I want to create an inventory list for a product, and one instance variable of said product has only three or four possible values, then I may as well use an Enum of static-valued strings as opposed to just a regular old open-ended string.


Here the word is not "enumeration" but "enumerated", as in "enumerated types." It's not a Java thing; this is the standard comp-sci name, and again, these were introduced into Java late in the game (JDK 1.5). If the Java gods were to start over, they'd leave out java.util.Enumeration, and include enumerated types (which use the "enum" keyword.)


2) Iterators and Enumeration, or at least my first (a) definition of enumeration: Aren't these interfaces? As in abstract class-like things which one implements, and can't instantiate? Why is it I always see these so-called 'interfaces' being instantiated within for()?
Quientessential example:
for(Iterator it = data.iterator(); itHasNext()


This is one of those Big Ideas which, once you grasp it, will go a long way towards making you into a Real Programmer. Yes, you cannot instantiate an interface; but you can instantiate a class that implements an interface, and you can manipulate it using a variable whose type is the interface type, without knowing the name of the class. Look hard at your example above. The "iterator()" method is returning an instance of some class; that class implements Iterator, and that's all we know about it. Furthermore, that's all we need to know! The "data" object is hiding some complexity from you! Since it knows the name of the class that's being returned, you yourself don't need to -- and that's a Good Thing.

Go have a look at the JDBC API (java.sql). See all those interfaces? A database driver takes the form of a set of classes that implement them. You, the user of the database, doesn't need to know the name of any of those classes -- the API is carefully designed to hide them from you. That means that your program can run with any set of classes that implements those interfaces -- i.e., with any JDBC driver, against any database, without changes! Cool, right?


3) Why use iterators/enumerators over just a simple for (Object a: ObjectList) {} statement? It seems to accomplish the same thing, and is far more legible? Java Head First started out with the simple for() version, but has recently made the swap to iterators, and I'm not sure I understand why.


The "for (Object a: ObjectList)" syntax was actually added in JDK 1.5, too; before that the explicit Iterator form was all you had. You need to recognize both, although much of the time you're right, you'll use the simpler form that hides the Iterator (yes, the simple form uses Iterator behind the scenes.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic