October 2018
Intermediate to advanced
370 pages
9h 15m
English
This is a parent interface in the interface hierarchy. Any data structure that contains a sequence of elements inherits from this interface. The Iterable interface provides an iterator, which is used to iterate over the elements.
The Iterable interface contains only one function, which returns the iterator:
public interface Iterable<out T> { public operator fun iterator(): Iterator<T>}
To expose the interface to the list, it is necessary to define it in the declaration of the list:
val iterableValues : Iterable <Int> = listOf(1,2,3,4,5)
Now, the iterableValues variable can access the iterator to iterate over the list:
val iterator = iterableValues.iterator()
The Iterator interface provides two functions:
Read now
Unlock full access