Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

kotlin
data structures
communitycreator

What is the ArrayList.retainAll() method in Kotlin?

Siddharth Singh

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The kotlin.collections package is part of Kotlin’s standard library, and it contains all collection types, such as Map, List, Set, etc.

The package provides the ArrayList class, which is a mutable list that uses a dynamic resizable array as the backing storage.

The retainAll() method

The ArrayList class contains the retainAll() method, which retains only the elements which are present in the specified input collection and removes all other elements from the array list.

Syntax

fun retainAll(elements: Collection<E>): Boolean

Arguments

  • This method takes a collection of elements as input.

Return value

  • This method returns true if any of the element is removed in the operation or false if the array list is not modified.

Things to note

  • ArrayList follows the sequence of insertion of elements.

  • It is allowed to contain duplicate elements.

The illustration below shows the function of the retainAll() method.

Retaining elements in a collection using the retainAll() method
fun main(args : Array<String>) {
val fruitList = ArrayList<String>()
fruitList.add("Orange")
fruitList.add("Apple")
fruitList.add("Grapes")
fruitList.add("Banana")
println("Printing ArrayList elements --")
println(fruitList)
var retainList = ArrayList<String>()
retainList.add("Apple")
retainList.add("Grapes")
fruitList.retainAll(retainList)
println("Printing ArrayList elements after calling retainAll() --")
println(fruitList)
}

Explanation

  • First, we create an empty array list named fruitList to store the strings.

  • Next, we add a few fruit names to the ArrayList object using the add() method, such as: "Orange", "Apple", "Grapes", and "Banana".

  • Next, we create a new list retainList and add two fruit names: "Apple" and "Grapes", using the add() method.

  • Next, we call the retainAll() method by passing retainList as input. It removes all elements of the array list except the elements present in the retainList.

  • The Arraylist elements and result of the retainAll() method are displayed using the print() function of the kotlin.io package.

RELATED TAGS

kotlin
data structures
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring