Skip to content

Instantly share code, notes, and snippets.

@greyaperez
Created February 27, 2014 04:10
Show Gist options
  • Save greyaperez/9244227 to your computer and use it in GitHub Desktop.
Save greyaperez/9244227 to your computer and use it in GitHub Desktop.
Implementation Example of Iterable Interface
/*
* Implement the java.lang.Iterable<E> interface
*/
public class MyCollection<E> implements Iterable<E>{
public Iterator<E> iterator() {
return new MyIterator<E>();
}
}
/*
* Skeleton of the MyIterator
*/
public class MyIterator <T> implements Iterator<T> {
public boolean hasNext() {
//implement...
}
public T next() {
//implement...;
}
public void remove() {
//implement... if supported.
}
}
/*
* Iterating Custom Collection "MyCollection"
*/
public static void main(String[] args) {
MyCollection<String> stringCollection = new MyCollection<String>();
for(String string : stringCollection){
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment