Scala Option and Iterators Example

Scala Option can be defined as container that holds zero or more elements of the given type. The option[T] can return Some[T] or None object which represents a missing value.

Consider an example of how to create a option;

Consider an example of how to assign a none value;

Create Student.scala class in Scala IDE as;

Here we are defining a class student with id, name, marks and gender as attributes. Create an object TestStudent in which we define findById method that returns an Option of type Student and findAll which retrieves the values from students map.

Now create another object s1.scala as
S1.scala

Below image shows the output produced when we run S1 as scala application.

Let’s look at some other examples for Scala Option.

Specifying default value

The default value can be specified using getOrElse method.

For example create a scala object as below:
GetorElse.scala

Below image shows the output produced as default value since gender was missing.

Scala-Option-Default-Example

The gender is specified as None while creating stud instance and hence the message “Gender not specified” is printed.

Pattern Matching

The optional values can be taken apart through pattern matching.

For instance:

KeyPatternMatch.scala

Output:

isEmpty() method

The isEmpty() method is used to check whether the option returns empty or not.

For example;
Empty.scala

It produces below output.

Useful Option Methods

def isDefined: Boolean → Returns true if the option is an instance of Some, else returns false

def or Null → Returns the option’s value if it is nonempty, or null if it is empty.

def isEmpty: Boolean → Returns true if the option is None, false otherwise.

def get: X → Returns the option’s value.

def foreach[Y](f: (Z) => Y): Unit → Apply the given procedure f to the option’s value, if it is nonempty.

Scala Iterators

An Iterator allows user to iterate over the collection thereby accessing all the elements. The core operations of iterator are next and hasNext. The next elements return the successive element and hasNext checks whether the element is present or not.

For example;

Create the scala object Iterator.scala as
Iterator.scala

Output:

Length of Iterator

The size or length methods can be used to find the number of the elements in the iterator.

For example,
CarLength.scala

Output:

Finding Minimum and Maximum Elements

The it.min and it.max methods are used to find the minimum and maximum elements in the iterator.

Create scala object MinMax.scala as;
MinMax.scala

Output:

Useful Iterator Methods

def addString(x: StringBuilder): StringBuilder → Returns the string builder x to which elements were appended.

def buffered: BufferedIterator[X] → Creates a buffered iterator from this iterator.

def foreach(f: (X) => Unit): Unit → Applies a function f to all values produced by this iterator.

def indexOf(elem: Y): Int → Returns the index of the first occurrence of the specified object in this iterable object.

def product: X → Multiplies the elements of this collection.

That’s all for Scala Option and Iterators, we will look into Scala Traits in the next tutorial.

By admin

Leave a Reply