The MutableIterable interface

This MutableIterable interface inherits the Iterable interface and contains its own mutable iterator. The mutable iterator provides a remove function, which removes the underlying element in the list. Take a look at the following diagram of the MutableIterable interface, which inherits the Iterable interface:

Create a mutable list and expose it with the mutableIterable interface. Get mutableIterator from the list and use the remove function to remove the underlying element of the list:

fun iterableIterface(){

val mutableList : MutableIterable<Int> = mutableListOf(1,2,3,4,5,6,7,8,9)
val mutableIter : MutableIterator <Int> = mutableList.iterator()

val element = 7
while (mutableIter.hasNext()) {
if(mutableIter.next() >= element) {
mutableIter.remove()
}
}

println(mutableList)
}

The mutable iterator extends the iterator interface so that we can use the hasNext() and next() functions, along with remove(). This example removes all elements in the list that are greater than or equal to seven.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.178.181