import java.util.*;
public Iterator<String> elements() {
return Collections.unmodifiableList(elements).iterator();
}
If you don't mind that the items are stored as a list, you can also use do:
public ListIterator<String> elements() {
return Collections.unmodifiableList(elements).listIterator();
}
If you want to allow callers to use the foreach syntax, you can return Iterable:
public Iterable<String> getElements() {
return Collections.unmodifiableList(elements);
}
And again, if you do not mind that the elements are returned as a list, this last decision could return List<String>
source
share