Scala | Option
The Option in Scala is referred to a carrier of single or no element for a stated type. When a method returns a value which can even be null then Option is utilized i.e, the method defined returns an instance of an Option, in place of returning a single object or a null.
Important points :
- The instance of an Option that is returned here can be an instance of Some class or None class in Scala, where Some and None are the children of Option class.
- When the value of a given key is obtained then Some class is generated.
- When the value of a given key is not obtained then None class is generated.
Example :
// Scala program for Option // Creating object object option { // Main method def main(args: Array[String]) { // Creating a Map val name = Map("Nidhi" -> "author", "Geeta" -> "coder") // Accessing keys of the map val x = name.get("Nidhi") val y = name.get("Rahul") // Displays Some if the key is // found else None println(x) println(y) } } |
Some(author) None
Here, key of the value Nidhi is found so, Some is returned for it but key of the value Rahul is not found so, None is returned for it.
Different way to take optional values
- Using Pattern Matching :
Example :
// Scala program for Option// with Pattern matching// Creating objectobjectpattern{// Main methoddefmain(args:Array[String]){// Creating a Mapvalname=Map("Nidhi"->"author","Geeta"->"coder")//Accessing keys of the mapprintln(patrn(name.get("Nidhi")))println(patrn(name.get("Rahul")))}// Using Option with Pattern// matchingdefpatrn(z:Option[String])=zmatch{// for 'Some' class the key for// the given value is displayedcaseSome(s)=>(s)// for 'None' class the below string// is displayedcaseNone=>("key not found")}}chevron_rightfilter_noneOutput:author key not found
Here, we have used Option with Pattern Matching in Scala.
- getOrElse() Method:
This method is utilized in returning either a value if it is present or a default value when its not present. Here, For Some class a value is returned and for None class a default value is returned.
Example:// Scala program of using// getOrElse method// Creating objectobjectget{// Main methoddefmain(args:Array[String]){// Using Some classvalsome:Option[Int]=Some(15)// Using None classvalnone:Option[Int]=None// Applying getOrElse methodvalx=some.getOrElse(0)valy=none.getOrElse(17)// Displays the key in the// class Someprintln(x)// Displays default valueprintln(y)}}chevron_rightfilter_noneOutput:15 17
Here, the default value assigned to the None is 17 so, it is returned for the None class.
- isEmpty() Method:
This method is utilized to check if the Option has a value or not.
Example:// Scala program of using// isEmpty method// Creating objectobjectcheck{// Main methoddefmain(args:Array[String]){// Using Some classvalsome:Option[Int]=Some(20)// Using None classvalnone:Option[Int]=None// Applying isEmpty methodvalx=some.isEmptyvaly=none.isEmpty// Displays true if there// is a value else falseprintln(x)println(y)}}chevron_rightfilter_noneOutput:false true
Here, isEmpty returns false for Some class as it non-empty but returns true for None as its empty.
Recommended Posts:
- Scala | Methods to Call Option
- Scala Map
- Set in Scala | Set-1
- Set in Scala | Set-2
- Scala | Either
- while and do while Loop in Scala
- Scala Map last() method with example
- Scala Map min() method with example
- Recursion in Scala
- Scala Map max() method with example
- Scala | REPL
- Scala | Tuple
- Scala | ArrayBuffer
- Scala | Arrays
- Scala | map() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



