To check if a List contains all the elements of another List in Kotlin, you can use the containsAll
method of the List interface.
In this example,
list1
and list2
containing integers.containsAll
method of list1
to check if it contains all elements of list2
.containsAll
method returns true
if all elements of list2
are present in list1
; otherwise, it returns false
.fun main() {
val list1 = listOf(1, 2, 3, 4, 5)
val list2 = listOf(2, 4)
val result = list1.containsAll(list2)
println("List 1 contains all elements of List 2: $result")
}
List 1 contains all elements of List 2: true
In this example,
list1
and list2
containing strings.containsAll
method of list1
to check if it contains all elements of list2
.containsAll
method returns true
if all elements of list2
are present in list1
; otherwise, it returns false
.fun main() {
val list1 = listOf("apple", "banana", "orange", "kiwi")
val list2 = listOf("banana", "kiwi")
val result = list1.containsAll(list2)
println("List 1 contains all elements of List 2: $result")
}
List 1 contains all elements of List 2: true
In this tutorial, we learned How to check if a List contains all the elements of Another List in Kotlin language with well detailed examples.