Category : Scala | Sub Category : Scala Programs | By Prasad Bonam Last updated: 2023-10-21 03:19:57 Viewed : 503
Scala provides a rich set of collection operations that can be used on various types of collections, including lists, sets, maps, sequences, and more. Some of the common collection operations in Scala include:
Transformations:
map
: Applies a function to each element in the collection and returns a new collection.flatMap
: Applies a function that returns a collection to each element and then flattens the result.filter
: Selects elements that satisfy a given predicate.Here are some transformation examples using common operations on collections in Scala:
Map Transformation:
scalaval numbers = List(1, 2, 3, 4, 5) val squaredNumbers = numbers.map(x => x * x) println(squaredNumbers)
Output:
scssList(1, 4, 9, 16, 25)
FlatMap Transformation:
scalaval words = List("Hello", "World") val letters = words.flatMap(_.toList) println(letters)
Output:
scssList(H, e, l, l, o, W, o, r, l, d)
Filter Transformation:
scalaval numbers = List(1, 2, 3, 4, 5) val evenNumbers = numbers.filter(_ % 2 == 0) println(evenNumbers)
Output:
scssList(2, 4)
GroupBy Transformation:
scalaval words = List("apple", "banana", "cherry", "blueberry", "blackberry") val groupedByLength = words.groupBy(_.length) println(groupedByLength)
Output:
scssMap(5 -> List(apple), 6 -> List(banana, cherry), 9 -> List(blueberry, blackberry))
Reverse Transformation:
scalaval numbers = List(1, 2, 3, 4, 5) val reversedNumbers = numbers.reverse println(reversedNumbers)
Output:
scssList(5, 4, 3, 2, 1)
These examples demonstrate how to perform various transformations on collections in Scala using common operations such as map
, flatMap
, filter
, groupBy
, and reverse
. The output demonstrates the results of each transformation applied to the corresponding collections.
Folding and Reducing:
foldLeft
and foldRight
: Combines the elements of the collection using a binary operator.reduceLeft
and reduceRight
: Reduces the elements of the collection using a binary operator.Aggregations:
sum
, min
, max
, product
: Compute the sum, minimum, maximum, or product of the elements in the collection.groupBy
: Groups elements by a key.Concatenation and Appending:
++
or concat
: Concatenates two collections.:+
or +:
: Appends an element to the end or beginning of a collection.Conversions:
toList
, toSet
, toMap
: Converts the collection to a list, set, or map, respectively.Element Access and Manipulation:
head
and last
: Access the first and last elements of a collection.tail
and init
: Returns a new collection without the first or last element, respectively.reverse
: Reverses the order of elements in the collection.distinct
: Returns a collection with distinct elements.These are just some of the common operations provided by the Scala collections API. Each collection type in Scala has its own set of specific operations along with the general operations that are applicable to all collections. Understanding these operations is crucial for effective manipulation and transformation of data in Scala.