How to use ArrayDeque.containsAll( ) method in Kotlin?
Overview
The containsAll() method checks if all the elements of the passed collection are present in the ArrayDeque object.
Syntax
fun containsAll(elements: Collection): Boolean
Parameter
This method takes the Collection as a parameter to check if it is present in this deque.
Return value
This method returns true if all the passed Collection elements are present in the deque. Otherwise, false is returned.
Code
The code below demonstrates how to use the containsAll() method:
fun main() {//create a new ArrayDeque which can have integer type as elementsvar deque: ArrayDeque<Int> = ArrayDeque<Int>()// add four entriesdeque.add(1)deque.add(2)deque.add(3)deque.add(4)println("\nThe deque is : $deque")val list1 = listOf(1,2);println("\nThe list1 is : $list1")println("deque.containsAll(list1) : " + deque.containsAll(list1));val list2 = listOf(1,5);println("\nThe list2 is : $list2")println("deque.containsAll(list2) : " + deque.containsAll(list2));}
Explanation
In the code above,
-
Line 3: We create a new
ArrayDequeobject with the namedeque. -
Line 6 to 9: We add four new elements(
1,2,3,4) to thedequeusing theadd()method. -
Line 13: We create a new
Listobject namedlist1with two elements[1,2]using thelistOf()method. -
Line 15: We use the
containsAll()method to check if all elements of thelist1are present in thedeque. In this case,trueis returned because all elements oflist1are present in thedeque.
* elements of deque - 1,2,3,4
* elements of list1 - 1,2
All elements of list1 are present in the deque.
-
Line 17: We create a new
Listobject withlist2with two elements[1,5]using thelistOf()method. -
Line 19: We use the
containsAll()method to check if all elements of thelist2are present in thedeque. In this case,falseis returned because element5oflist2is not present in thedeque.
* elements of deque - 1,2,3,4
* elements of list2 - 1,5
Element 5 of list2 is not present in the deque.