

In this case, we’re using the filterIsInstance() function to return a List of elements of type Integer. Val filterList = countr圜ode.filterIsInstance()ĪssertTrue(ntainsAll(listOf(49,91,81,61))) The filterIsInstance() function can be used to return a List of elements of a specified type: val countr圜ode = listOf("Germany", 49, null, "India", 91, "Japan", 81, "Brazil", null, "Australia", 61) In this example, the filterIndexedTo() function appends the resulting two elements matching the predicate to the mutableList. It then returns a List of elements matching the given predicate: val filterList = countries.filterIndexed )ĪssertTrue(ntainsAll(listOf("United States","Canada","Germany","Australia"))) The predicate of this function uses both the index and the element as arguments. Println(".mutableList.subList(1,2).We can use the filterIndexed() function to utilize the element position in the filter. mutableList2 after mutableList2.clear().")

Println(".mutableList.remove(\"Vijay\").") Println(".mutableList.addAll(mutableList2).")

Var mutableList4 = mutableListOf("Ajay","Dharmesh","Ashu") Var mutableList3 = mutableListOf("Dharmesh","Umesh") Var mutableList2 = mutableListOf("Rohan","Raj") Let's see an example of MutableList using mutableListOf() function and traverse its elements. The returned list is backed by current list, so non-structural changes in the returned list are reflected in current list, and vice-versa. It returns part of list from specified fromIndex (inclusive) to toIndex (exclusive) from current list. It replaces the element and add new at given index with specified element. It retains all the elements within the current collection which are present in given collection.Ībstract operator fun set(index: Int, element: E): E It removes element at given index from the list.Ībstract fun retainAll(elements: Collection): Boolean It removes all the elements from the current list which are also present in the specified collection. It removes the specified element if it present in current collection.Ībstract fun removeAll(elements: Collection): Boolean It returns a list iterator starting from specified index over the elements in list in proper sequence. It returns a list iterator over the elements in proper sequence in current list.Ībstract fun listIterator(index: Int): MutableListIterator It removes all the elements from this collection.Ībstract fun listIterator(): MutableListIterator It adds all the elements of given collection to current collection. It adds the given element to the collection.Ībstract fun addAll(elements: Collection): Boolean Some methods of MutableList interface are mention below. There are several methods are available in MutableList interface. Interface MutableList : List, MutableCollection (source)
