Scala List sum() operation with examples

 165 total views,  1 views today

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

Scala List

  • List() is a collection to store values in an ordered fashion. It allows store duplicate values.
  • List() extends LinearSeq() trait and it is based on Last In First Out model.
  • List() is part of both immutable and mutable collection package of Scala. Immutable List allows us to apply a function on List but resulting in the creation of a new list every time. It doesn’t allow us the source. On the other hand, the mutable list allows modifying the source itself.
  • List() is a collection to store values in an ordered fashion.

sum() operation is a useful operation of the Scala List which allows us to retrieve the sum of elements of the list. When you call this operation on a Scala list, it returns a sum value of list elements.It sums all of values of the List.

sum() Syntax

def sum: B

Sums up the elements of this collection.

Scala List sum() operation  example 1

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

    //create List
    var list: List[Int] = List(23, 12, 43, 5, 6, 79)

    //Find sum
    println(list.sum)

  }
}

Output

168

ReferencesScala List sum operation

I hope you like this tutorial and you were able to understand Scala list sum 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!.