what is iterator
Download
Skip this Video
Download Presentation
What is Iterator

Loading in 2 Seconds...

play fullscreen
1 / 9

What is Iterator - PowerPoint PPT Presentation


  • 133 Views
  • Uploaded on

What is Iterator. Category: Behavioral Generic Way to Traverse Collection Not Related to Collection Implementation Details Not Related to the direction/fashion with which the collection is traversed. How to Use Iterator. Implementation Details

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'What is Iterator' - anaya


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
what is iterator
What is Iterator
  • Category: Behavioral
  • Generic Way to Traverse Collection
  • Not Related to Collection Implementation Details
  • Not Related to the direction/fashion with which the collection is traversed
how to use iterator
How to Use Iterator
  • Implementation Details
    • Have a class (Class A) with which iteration makes sense
    • Create a class (Class B) that implements the java.util.Iterator interface
    • Class B should manage the iteration state over Class A for a single client.
  • Usage Details
    • Call constructing iterator() method on collection object.
    • Use Iterator object returned to iterate elements (next(), hasNext())
how to use iterator1
How to Use Iterator
  • Example Iterator implementation (note: similar to actual Java implementation)

// Note: Some features are not implemented…

public class VectorIterator implements java.util.Iterator {

private Vector collection;

private int cursor=0;

public VectorIterator(Vector toIterate) {

collection = toIterate;

}

public boolean hasNext() {

return (cursor < collection.size());

}

public void next() {

cursor++;

Object next = collection.get(cursor);

}

}

how to use iterator2
Traditional Looping

Vector aVector = new Vector();

// load vector with elements

// Implementation omitted

for(int i=0; i

Object anElem = aVector.get(i);

}

Iterator Looping

Vector aVector = new Vector();

// load vector with elements

// Implementation omitted

Iterator elements = aVector.iterator();

while(elements.hasNext()) {

Object anElem = elements.next();

}

How to Use Iterator
class diagram
Class Diagram

From (Stephen Stelting, Olav Maassen, Applied Java™ Patterns)

why iterator
Why Iterator
  • Hides details of Collection Traversal
  • Avoids the ugly, and sometimes bug-prone index/cursor management (int i=…)
  • The iterator pattern can be used to disperse the cost of expensive object construction
in the real world
In the Real World
  • Java has built in Iterator Support for the Collections Library (java.util.Iterator)
  • Vector, ArrayList, LinkedList, HashSet, TreeSet, etc (Collection.iterator() method).
  • Iterator may be traversing an Array, a LinkedList, or even a binary tree, but the code using the library knows no difference.
  • Developers can reuse the Iterator interface for their own libraries/implementations
in the real world1
In the Real World
  • Iterator can be used to defer expensive reads such as database calls and file parsing until it is actually necessary.
  • In combination with the factory pattern, collection type choices can be completely abstracted from the program, and into configuration files.
advanced java examples
Advanced Java Examples

public class CollectionsExample {

public static void main(String[] args) {

Collection collectionA = null;

collectionA = createCollection(args);

iterateCollection(collectionA);

}

public static Collection createCollection(String[] args) {

Collection collection = null;

if(args[0].equals(“Vector”)) {

// returns a index based list walking iterator

collection = new Vector();

}

else if(args[0].equals(“TreeSet”)) {

// returns a tree walking iterator.

collection = new TreeSet();

}

return collection;

}

// Loop code stays the same regardless of the implementation

public static void iterateCollection(Collection collection) {

Iterator elements = collection.iterator();

while(elements.hasNext()) {

Object anObj = elements.next();

}

}

}

ad