- Scala Collections - Discussion
- Scala Collections - Useful Resources
- Scala Collections - Quick Guide
- Scala Collections - zip
- Scala Collections - scan
- Scala Collections - reduce
- Scala Collections - partition
- Scala Collections - map
- Scala Collections - foldRight
- Scala Collections - foldLeft
- Scala Collections - fold
- Scala Collections - flatten
- Scala Collections - flatMap
- Scala Collections - find
- Scala Collections - filter
- Scala Collections - dropWhile
- Scala Collections - drop
- Scala Collections - Stream
- Scala Collections - Stack
- Scala Collections - Seq
- Scala Collections - Tuple
- Scala Collections - Queue
- Scala Collections - Option
- Scala Collections - Iterator
- Scala Collections - ListMap
- Scala Collections - HashMap
- Scala Collections - Map
- Scala Collections - TreeSet
- Scala Collections - HashSet
- Scala Collections - BitSet
- Scala Collections - Set
- Scala Collections - Vector
- Scala Collections - ListSet
- Scala Collections - ListBuffer
- Scala Collections - List
- Scala Collections - ArrayBuffer
- Scala Collections - Array using Range
- Scala Collections - Multi-Dimensional Array
- Scala Collections - Array
- Scala Collections - Environment Setup
- Scala Collections - Overview
- Scala Collections - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Scala Collections - Option
Scala Option[ T ] is a container for zero or one element of a given type. An Option[T] can be either Some[T] or None object, which represents a missing value. For instance, the get method of Scala s Map produces Some(value) if a value corresponding to a given key has been found, or None if the given key is not defined in the Map.
Option type is used frequently in Scala programs and you can compare this with the null value available in Java which indicate no value. For example, the get method of java.util.HashMap returns either a value stored in the HashMap, or null if no value was found.
Let s say we have a method that retrieves a record from the database based on a primary key.
def findPerson(key: Int): Option[Person]
The method will return Some[Person] if the record is found but None if the record is not found. Let us follow the following program.
Example
object Demo { def main(args: Array[String]) { val capitals = Map("France" -> "Paris", "Japan" -> "Tokyo") println("capitals.get( "France" ) : " + capitals.get( "France" )) println("capitals.get( "India" ) : " + capitals.get( "India" )) } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
>scalac Demo.scala >scala Demo
Output
capitals.get( "France" ) : Some(Paris) capitals.get( "India" ) : None
The most common way to take optional values apart is through a pattern match. For example try the following program.
Example
object Demo { def main(args: Array[String]) { val capitals = Map("France" -> "Paris", "Japan" -> "Tokyo") println("show(capitals.get( "Japan")) : " + show(capitals.get( "Japan")) ) println("show(capitals.get( "India")) : " + show(capitals.get( "India")) ) } def show(x: Option[String]) = x match { case Some(s) => s case None => "?" } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
>scalac Demo.scala >scala Demo
Output
show(capitals.get( "Japan")) : Tokyo show(capitals.get( "India")) : ?
Using getOrElse() Method
Following is the example program to show how to use getOrElse() method to access a value or a default when no value is present.
Example
object Demo { def main(args: Array[String]) { val a:Option[Int] = Some(5) val b:Option[Int] = None println("a.getOrElse(0): " + a.getOrElse(0) ) println("b.getOrElse(10): " + b.getOrElse(10) ) } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
>scalac Demo.scala >scala Demo
Output
a.getOrElse(0): 5 b.getOrElse(10): 10
Using isEmpty() Method
Following is the example program to show how to use isEmpty() method to check if the option is None or not.
Example
object Demo { def main(args: Array[String]) { val a:Option[Int] = Some(5) val b:Option[Int] = None println("a.isEmpty: " + a.isEmpty ) println("b.isEmpty: " + b.isEmpty ) } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
>scalac Demo.scala >scala Demo
Example
a.isEmpty: false b.isEmpty: trueAdvertisements