iterable java
// Java code to illustrate the use of iterator
import java.io.*;
import java.util.*;
class Test {
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
// Iterator to traverse the list
Iterator iterator = list.iterator();
System.out.println("List elements : ");
while (iterator.hasNext())
System.out.print(iterator.next() + " ");
System.out.println();
}
}
class CustomDataStructure implements Iterable<> {
// code for data structure
public Iterator<> iterator() {
return new CustomIterator<>(this);
}
}
class CustomIterator<> implements Iterator<> {
// constructor
CustomIterator<>(CustomDataStructure obj) {
// initialize cursor
}
// Checks if the next element exists
public boolean hasNext() {
}
// moves the cursor/iterator to next element
public T next() {
}
// Used to remove an element. Implement only if needed
public void remove() {
// Default throws UnsupportedOperationException.
}
}