Using an Anonymous Class to return and create Iterator
import java.util.ArrayList; import java.util.Iterator; class TitleList { private ArrayList<String> titleList = new ArrayList<>(); public void addTitle(String title) { titleList.add(title);// w ww . j a v a 2s . c om } public void removeTitle(String title) { titleList.remove(title); } public Iterator<String> titleIterator() { // An anonymous class Iterator<String> iterator = new Iterator<String>() { int count = 0; @Override public boolean hasNext() { return (count < titleList.size()); } @Override public String next() { return titleList.get(count++); } }; // Anonymous inner class ends here return iterator; } } public class Main { public static void main(String[] args) { TitleList tl = new TitleList(); tl.addTitle("A"); tl.addTitle("tutorial from book2s.com"); Iterator<String> iterator = tl.titleIterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } }