Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

kotlin
communitycreator
data structures

What is the ArrayList.addAll() 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. It contains all collection types, including 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 addAll() method

The ArrayList class contains the addAll() method, which adds all elements of the specified input collection to the end of the array list.

Syntax

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

Arguments

This method takes a collection of elements as input.

Return value

This method returns the boolean value true if the list is modified during the addAll() operation. Otherwise, it returns false.

Things to note

  • ArrayList follows the sequence of insertion of elements.

  • It is allowed to contain duplicate elements.

  • The elements are appended in the order they appear in the specified collection.

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

Adding elements of a collection to an array list using the addAll() method

Code

The ArrayList class is present in the kotlin.collection package and is imported by default in every Kotlin program.

fun main(args : Array<String>) {
val countryList = ArrayList<String>()
countryList.add("India")
countryList.add("US")
println("Printing ArrayList elements--")
println(countryList)
val islandList = ArrayList<String>()
islandList.add("Maldives")
islandList.add("Mauritius")
countryList.addAll(islandList)
println("Printing ArrayList elements after calling addAll()--")
println(countryList)
}

Explanation

  • First, we create an empty ArrayList to store the strings.

  • Next, we add a few country names to the ArrayList object using the add() method, such as: "India"and "US".

  • Next, we create another array list of strings and add few island names to the new ArrayList object using the add() method, such as: "Maldives"and "Mauritius".

  • Next, we call the addAll() method by passing the island names array list as the input parameter. It then adds all the elements of the island names array list to the country name array list.

  • The Arraylist elements are displayed before and after calling the addAll() method by using the print() function of the kotlin.io package. The list contains four names after calling the addAll() method.

RELATED TAGS

kotlin
communitycreator
data structures

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