Scala Collections: Part 2 - A few basic operations

In the first part of this series, I went through the Scala Collections hierarchy and how it's put together.

In following posts, I'll go through a lot of the operations you can use on collections. I will not cover all of them, so for a full overview, please take a look at the Collection API.

In this first post about the collection operations, I'll quickly go through a few of the simpler ones. Still, it's important to cover them since they are significant to the Scala Collections.


def exists(p: ((A, B)) ⇒ Boolean): Boolean  

Tests whether a predicate holds for some of the elements of this traversable collection.


// Given a list of programming languages.
// Check if "Scala" exists in the list.
List("Scala", "Java", "Haskell") exists (x => x == "Scala")

// Output: true

// Given a map of languages and their creators.
// Check if Odersky created a language.
Map("Scala" -> "Odersky", "Prolog" -> "Colmerauer") exists (x => x._2 == "Odersky")

// Output: true


def forall(p: (A) ⇒ Boolean): Boolean  

Tests whether a predicate holds for all elements of this iterable collection.


// Given a list of integers.
// Check if vector only contains positive integers.
Vector(1, 2, -4, 3) forall (x => x >= 0)

// Output: false


def filter(p: (A) ⇒ Boolean): Traversable[A]  

Selects all elements of this collection which satisfy a predicate.


// Given a list of tuples (language, released).
// Get all languages created after 2000.
List(("Clojure", 2007), ("Haskell", 1990), ("Scala", 2003)) filter (x => x._2 >= 2000)

// Output: List((Clojure,2007), (Scala,2003))


def map[B](f: (A) ⇒ B): Map[B]  

Builds a new collection by applying a function to all elements of this collection.


// Given a list of integers.
// Get a list with all the elements squared.
List (1,2,3,4) map ( x => x * x)

// Output: List(1, 4, 9, 16)


    def take(n: Int): Traversable[A] 

Selects first n elements.

Great for working with infinite recursions and streams.


// Given an infinite recursive method creating a stream of even numbers.
 def even: Stream[Int] = {
     def even0(n: Int): Stream[Int] =
         if (n%2 == 0) n #:: even0(n+1)
         else even0(n+1)

// Get a list of the 4 first even numbers.
even take (4) toList

// Output: List(2, 4, 6, 8)


def groupBy[K](f: ((A, B)) ⇒ K): Map[K, Map[A, B]]  

Partitions this traversable collection into a map of traversable collections according to some discriminator function.


// Given a list of numbers.
// Group them as even and odd numbers.
List(1,2,3,4) groupBy (x => if (x%2 == 0) "even" else "odd")

// Output: Map(odd -> List(1, 3), even -> List(2, 4))


def init: Traversable[A]  

Selects all elements except the last.


List (1,2,3,4) init

// Output: List(1, 2, 3)


def drop(n: Int): Traversable[A]  

Selects all elements except first n ones.


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

// Output: List(3, 4)


We've now covered a few of the more self explanatory operations. In the following post we'll continue by looking at some operations that needs a bit more explaining, but brings a wide range of possibilities.

Stay tuned!

Parts in this series:

Enjoyed the post?

If you don't want to miss future posts, make sure to subscribe