Scala list apply() operation with examples

 119 total views,  3 views today

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

Scala apply() operation allows us to take/retrieve the elements of the given index. apply() operation accepts the index and retrieves the elements of that particular index. apply() operation in Scala is similar to get() operation in Java list to retrieve the element based on the given index.

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

apply() Syntax

def apply(n: Int): A

Get the element at the specified index.

Scala List apply() examples

In this apply() example, we have created a list and called apply() method by passing the required index.

object ApplyTest {

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

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

    //apply()
    println(list.apply(3))
    println(list.apply(4))
    println(list.apply(5))

  }
}

Output

4
5
6

Let us see a few more examples of apply() operation.

object ApplyTest {

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

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

    println(list.apply(0))
    println(list.apply(0))
    println(list.apply(1))

  }
}

Output

1
1
2

Let us see a negative scenario where we provide an invalid index.

object ApplyTest {

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

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

    println(list.apply(10))
    println(list.apply(0))
    println(list.apply(1))
  }
}

Output

Exception in thread "main" java.lang.IndexOutOfBoundsException: 10
 at scala.collection.LinearSeqOptimized.apply(LinearSeqOptimized.scala:63)
 at scala.collection.LinearSeqOptimized.apply$(LinearSeqOptimized.scala:61)

We have an alternative to apply() operation.Instead of using apply() ,we can simply call list(1),list(2) to get the elements.

object ApplyTest {

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

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

    println(list.apply(1))
    println(list.apply(4))
    println(list.apply(5))

    println(list(1))
    println(list(4))
    println(list(5))

  }
}

Output

2
5
6
2
5
6

ReferencesScala apply() 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 apply() 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!.

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.