On 3/7/19 9:41 AM, Peter Levart wrote:
There is a benefit in the runtime though. The code can decide what to do with the passed-in Iterable depending on it implementing IterableOnce or not. Much like what RandomAccess interface does to List(s). The code can decide to dump the iterable into a List and iterate the List multiple times if the Iterable implements IterableOnce or do direct multiple iteration on the passed-in Iterable if it doesn't.
Expanding on this further, there could be help for multi-pass iteration in the Iterable(Once) interfaces. For example:
public interface Iterable<T> { Iterator<T> iterator(); default Iterable<T> toMultipassIterable() { return this; } } and then: public interface IterableOnce<T> extends Iterable<T> { @Override default Iterable<T> toMultipassIterable() { List<T> list = new ArrayList<>(); for (T t : this) { list.add(t); } return list; } } ... so any new code that needs multiple passes can do so easily. Regards, Peter