Category : Scala | Sub Category : Scala Programs | By Prasad Bonam Last updated: 2020-10-08 13:16:01 Viewed : 540
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(0, 10)
· 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.
package runnerdev
object ListExampleApp extends App {
var listVal = List(1, 3, 5, "dog", 9, "one", 23, "Cat", 4)
var listVal2: List[Int] = List(1, 2, 3, 4, 5)
println(listVal)
println(listVal2)
}
compile and run the above example as follows
scala> scalac ListExampleApp.scala
scala> scala ListExampleApp
List(1, 3, 5, dog, 9, one, 23, Cat, 4)
List(1, 2, 3, 4, 5)
In Scala, a List
is a linear, immutable collection that represents an ordered sequence of elements. Lists are similar to arrays, but unlike arrays, they cannot be modified after creation. Scala lists are recursive in nature, consisting of a head (the first element) and a tail (the rest of the list), which is another list. Here is an overview of Scala lists and how to work with them:
1. Creating a List:
You can create a list by providing a comma-separated list of elements enclosed in parentheses or by using the List()
constructor:
scalaval myList = List(1, 2, 3, 4, 5) val emptyList = List.empty[Int] // An empty list of integers
2. Accessing Elements:
You can access elements in a list using the apply
method (similar to array indexing):
scalaval firstElement = myList(0) // Access the first element (1)
3. List Operations: Lists support various operations, including:
Concatenation: You can concatenate two lists using the :::
operator or the ++
operator.
scalaval concatenatedList = myList ::: List(6, 7, 8)
Head and Tail: You can access the head and tail of a list using head
and tail
methods.
scalaval headElement = myList.head // Access the first element (1) val tailList = myList.tail // Access the list without the first element (List(2, 3, 4, 5))
Prepending Elements: You can prepend elements to a list using the ::
operator or the +:
operator.
scalaval newList = 0 :: myList // Prepends 0 to the list (List(0, 1, 2, 3, 4, 5))
4. List Methods:
Lists provide various methods for common operations, such as map
, filter
, foldLeft
, foldRight
, and more. These methods return new lists or values based on the transformation or filtering applied to the elements.
scalaval squaredList = myList.map(x => x * x) // Square each element val filteredList = myList.filter(_ % 2 == 0) // Filter even elements
5. Immutability: Lists are immutable, meaning you cannot modify their elements. Instead, you create new lists when performing operations.
6. Pattern Matching: Pattern matching is a powerful feature in Scala often used with lists to destructure and process their elements.
scalamyList match { case head :: tail => println(s"Head: $head, Tail: $tail") case Nil => println("Empty list") }
7. List Comprehensions: Scala supports list comprehensions, which allow you to create lists through concise expressions.
scalaval evenNumbers = for (n <- myList if n % 2 == 0) yield n
Scalas List
is an essential collection type that provides a functional and expressive way to work with sequences of data. It is widely used in Scala programming for a variety of tasks.
Here are examples of working with Scala List
collections along with explanations and their respective outputs:
1. Creating a List:
scalaval myList = List(1, 2, 3, 4, 5)
In this example, we create a list called myList
containing integers from 1 to 5.
2. Accessing Elements:
scalaval firstElement = myList(0) // Access the first element (1)
This code accesses the first element of the list using indexing. The result is firstElement
, which is equal to 1.
3. List Operations:
scalaval concatenatedList = myList ::: List(6, 7, 8)
Here, we concatenate myList
with another list to create a new list, concatenatedList
.
scalaval headElement = myList.head // Access the first element (1) val tailList = myList.tail // Access the list without the first element (List(2, 3, 4, 5))
These lines show how to access the head (first element) and tail (rest of the list) of myList
.
scalaval newList = 0 :: myList // Prepends 0 to the list (List(0, 1, 2, 3, 4, 5))
We prepend an element (0) to myList
using the ::
operator.
4. List Methods:
scalaval squaredList = myList.map(x => x * x) // Square each element
This code creates a new list, squaredList
, by applying a mapping function to each element of myList
.
scalaval evenNumbers = myList.filter(_ % 2 == 0) // Filter even elements
Here, we create a new list, evenNumbers
, by filtering the even elements from myList
.
5. Pattern Matching:
scalamyList match { case head :: tail => println(s"Head: $head, Tail: $tail") case Nil => println("Empty list") }
This code uses pattern matching to destructure and process the elements of myList
. It prints the head and tail of the list.
6. List Comprehensions:
scalaval squaredEvens = for (n <- myList if n % 2 == 0) yield n * n
This line demonstrates a list comprehension, which generates a new list, squaredEvens
, containing the squares of even numbers from myList
.
These examples showcase various operations and methods that you can perform with Scala List
collections, making it a powerful tool for working with sequences of data.