Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

arraydeque
containsall
kotlin
communitycreator

How to use ArrayDeque.containsAll( ) method in Kotlin?

Programming Bytes

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 elements
  var deque: ArrayDeque<Int> = ArrayDeque<Int>()

  // add four entries
  deque.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));

}
Using the containsAll() method

Explanation

In the code above,

  • Line 3: We create a new ArrayDeque object with the name deque.

  • Line 6 to 9: We add four new elements(1,2,3,4) to the deque using the add() method.

  • Line 13: We create a new List object named list1 with two elements [1,2] using the listOf() method.

  • Line 15: We use the containsAll() method to check if all elements of the list1 are present in the deque. In this case, true is returned because all elements of list1 are present in the deque.

* 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 List object with list2 with two elements [1,5] using the listOf() method.

  • Line 19: We use the containsAll() method to check if all elements of the list2 are present in the deque. In this case, false is returned because element 5 of list2 is not present in the deque.

* elements of deque - 1,2,3,4
* elements of list2 - 1,5

Element 5 of list2 is not present in the deque.

RELATED TAGS

arraydeque
containsall
kotlin
communitycreator
RELATED COURSES

View all Courses

Keep Exploring