Scala Collections - List

Category : Scala | Sub Category : Scala Programs | By Runner Dev Last updated: 2020-10-08 18:46:01 Viewed : 219


Scala Collections - List 

The Scala List class — scala.List — holds a sequenced, linear list of items. In a List, each element must be of the same type. 

the Scala List class hierarchy:

·        The  scala.List class is a pointer to the scala.collection.immutable.List class

·        The List class extends LinearSeq with Product (and some others)

·        The trait LinearSeq extends Seq

·        The trait Seq extends Iterable

·        Iterable extends trait Traversable 

Scala offers many ways to populate lists. Here are just a few:

·        val nums = List.range(010)

·        val nums = (1 to 10 by 2).toList

·        val letters = (a to f).toList

·        val letters = (a to f by 2).toList

Example:

Following example illustrates about Scala collection - List

Save the file as −  ListExampleApp.scala.

 ListExampleApp.scala 

 

package runnerdev

object ListExampleApp extends App {

  var listVal = List(135"dog"9"one"23"Cat"4)

  var listVal2List[Int] = List(12345)

  println(listVal)

  println(listVal2)

}

 

 

compile and run the above example as follows  

scala> scalac ListExampleApp.scala

scalascala ListExampleApp

 Output:

List(1, 3, 5, dog, 9, one, 23, Cat, 4)

List(1, 2, 3, 4, 5)

Search
Sub-Categories
Related Articles

Leave a Comment: