Kotlin List containsAll()
Syntax & Examples
Syntax of List.containsAll()
The syntax of List.containsAll() function is:
abstract fun containsAll(elements: Collection<E>): Boolean
This containsAll() function of List checks if all elements in the specified collection elements
are contained in this collection.
✐ Examples
1 Example
In this example,
- We create a list named
list1
containing the characters'a', 'p', 'p', 'l', 'e'
. - We also create another list named
list2
containing the characters'p', 'l'
. - Then we check if
list1
contains all elements oflist2
using the ListcontainsAll()
function. - The result, which is a Boolean value, is stored in
result
. - Since
list1
contains all the elements of thelist2
, the expressionlist1.containsAll(list2)
returnstrue
. - Finally, we print the value of result to standard output using print statement.
Kotlin Program
fun main(args: Array<String>) {
val list1 = listOf("a", "p", "p", "l", "e");
val list2 = listOf("p", "l");
val result = list1.containsAll(list2);
print(result);
}
Output
true
2 Example
In this example,
- We create a list named
list1
containing the integers10, 20, 30
. - We also create a list named
list2
containing the integers20, 30
. - Then we check if
list1
contains all elements oflist2
using the ListcontainsAll()
function. - The result, which is a Boolean value, is stored in
result
. - Since
list1
contains all the elements of thelist2
, the expressionlist1.containsAll(list2)
returnstrue
. - Finally, we print the value of result to standard output using print statement.
Kotlin Program
fun main(args: Array<String>) {
val list1 = listOf(10, 20, 30);
val list2 = listOf(20, 30);
val result = list1.containsAll(list2);
print(result);
}
Output
true
3 Example
In this example,
- We create a list named
list1
containing the integers10, 20, 30
. - We also create a list named
list2
containing the integers20, 30
. - Then we check if
list1
contains all elements oflist2
using the ListcontainsAll()
function. - The result, which is a Boolean value, is stored in
result
. - Since
list1
does not contain all the elements of thelist2
, the expressionlist1.containsAll(list2)
returnsfalse
. - Finally, we print the value of result to standard output using print statement.
Kotlin Program
fun main(args: Array<String>) {
val list1 = listOf(10, 20, 30);
val list2 = listOf(40, 80);
val result = list1.containsAll(list2);
print(result);
}
Output
false
Summary
In this Kotlin tutorial, we learned about containsAll() function of List: the syntax and few working examples with output and detailed explanation for each example.