Scala list contains() and containsSlice() operation with examples

 271 total views,  5 views today

Welcome to the Scala series. In this tutorial, you will learn about the Scala List contains()  and containsSlice() operation with examples.

Scala contains()

Scala contains() operation allows us to check if the list has a particular element or not.contains() returns a boolean value.If the list has the given element then it returns true else false.

Scala contains() operation is applicable to all types of Scala collection.

contains() Syntax

def contains[A1 >: A](elem: A1): Boolean

Tests whether this list contains a given value as an element.

Scala List contains() examples

In this contains() example, we have created a list and have applied contains to check if the given elements are available in the list.

object ContainsTest {

  def main(args: Array[String]): Unit = {

    //create a List
    var list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8)

    //Check contains
    println(list.contains(1))
    println(list.contains(13))
    println(list.contains(3))

  }
}

Output

true
false
true

Scala containsSlice()

Scala containsSlice() operation allows us to check if the list has a particular sequence of elements.contains() returns a boolean value.If the list has the given sequence of elements then it returns true else false.

Scala containsSlice() operation is applicable to all types of Scala collection.

containsSlice() Syntax

def containsSlice[B](that: collection.Seq[B]): Boolean

Tests whether this sequence contains a given sequence as a slice.

Scala List containsSlice() examples

object ContainsSliceTest {

  def main(args: Array[String]): Unit = {

    //create a List
    var list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8)

    //Check containsSlice
    println(list.containsSlice(List(1, 2, 3)))
    println(list.containsSlice(List(6, 7, 8)))
    println(list.containsSlice(List(4, 5, 7)))

  }
}

Output

true
true
false

ReferencesScala contains() and containsSlice() operation

Would you like to learn more about Scala features, please check out Scala – a complete guide series. In this tutorial guide, you will find useful Scala functions that can be applied to the collection.

I hope you like this tutorial and you were able to understand the Scala list contains() and containsSlice() operation with examples.

Thanks for reading and please give us a thumb up and comment below.

 

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.

2 thoughts on “Scala list contains() and containsSlice() operation with examples

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.