Common collection operations in scala

Category : Scala | Sub Category : Scala Programs | By Prasad Bonam Last updated: 2023-10-21 03:19:57 Viewed : 232

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:

  1. 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:

      1. Map Transformation:

        val numbers = List(1, 2, 3, 4, 5) val squaredNumbers = => x * x) println(squaredNumbers)


        List(1, 4, 9, 16, 25)
      2. FlatMap Transformation:

        val words = List("Hello", "World") val letters = words.flatMap(_.toList) println(letters)


        List(H, e, l, l, o, W, o, r, l, d)
      3. Filter Transformation:

        val numbers = List(1, 2, 3, 4, 5) val evenNumbers = numbers.filter(_ % 2 == 0) println(evenNumbers)


        List(2, 4)
      4. GroupBy Transformation:

        val words = List("apple", "banana", "cherry", "blueberry", "blackberry") val groupedByLength = words.groupBy(_.length) println(groupedByLength)


        Map(5 -> List(apple), 6 -> List(banana, cherry), 9 -> List(blueberry, blackberry))
      5. Reverse Transformation:

        val numbers = List(1, 2, 3, 4, 5) val reversedNumbers = numbers.reverse println(reversedNumbers)


        List(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.

  2. 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.
  3. Aggregations:

    • sum, min, max, product: Compute the sum, minimum, maximum, or product of the elements in the collection.
    • groupBy: Groups elements by a key.
  4. Concatenation and Appending:

    • ++ or concat: Concatenates two collections.
    • :+ or +:: Appends an element to the end or beginning of a collection.
  5. Conversions:

    • toList, toSet, toMap: Converts the collection to a list, set, or map, respectively.
  6. 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.

Related Articles

Leave a Comment: