What is the contains() method in Kotlin?
Overview
In Kotlin, the contains method is used to check if the ArrayDeque contains a specific value or not.
Syntax
fun contains(element: E): Boolean
Parameter value
This method takes the argument, element, which represents the value we want to find.
Return value
This method returns True if the passed value is equal to any of the values present in the ArrayDeque.
Code example
The code given below demonstrates how we can check if the ArrayDeque contains a specific value:
fun main() {// create a new deque which can have string elementsvar deque = ArrayDeque<String>();// add three elements to dequeudeque.add("one");deque.add("two");deque.add("three");println("The deque is " + deque);// check if the deque contains the element oneprintln("\ndeque.contains('one'): " + deque.contains("one"));// check if the deque contains the element fiveprintln("\ndeque.contains('five'): " + deque.contains("five"));}
Code explanation
-
Line 3: We create an
ArrayDequewith the namedeque. -
Lines 6–8: We use the
addmethod to add three elements to thedeque. Now, thedequeis[one, two, three]. -
Line 12: We use the
containsmethod to check if thedequecontains the elementone. The elementoneis present in thedeque, sotrueis returned. -
Line 15: We use the
containsmethod to check if thedequecontains the elementfive. The elementfiveis not present in thedeque, sofalseis returned.