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.
There are a few methods that we can Call on Scala Option.
- def get: A
This method is utilized to return an Option’s value.
Example:// Scala program of using// get method// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){// Using Some classvalsome:Option[Int]=Some(20)// Applying get methodvalx=some.get// Displays the valueprintln(x)}}Output:20
Here, get method cannot be applied to the None class as it will show an exception.
- def productArity: Int
This method is utilized to return the size of the Option’s value.
Example:// Scala program of returning// the size of the value// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){// Using Some classvalsome:Option[Int]=Some(20)// Applying productArity// methodvalx=some.productArity// Displays the size of// the Option's valueprintln(x)}}Output:1
- def productElement(n: Int): Any
This method is utilized to return the n-th element of the stated product and here indexing starts from zero.
Example:// Scala program of returning// the n-th element of the// product// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){// Using Some classvalsome:Option[Int]=Some(20)// Applying productElement// methodvalx=some.productElement(0)// Displays the elementprintln(x)}}Output:20
Here, None will show an exception.
- def exists(p: (A) => Boolean): Boolean
When the value of the Option satisfies the stated condition then, this method returns true else returns false.
Example:// Scala program of the method// 'exists'// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){// Using Some classvalsome:Option[Int]=Some(30)// Applying exists methodvalx=some.exists(y=>{y%3==0})// Displays true if the condition// given is satisfied else falseprintln(x)}}Output:true
Here, the condition stated is satisfied so, true is returned.
- def filter(p: (A) => Boolean): Option[A]
This method is utilized to return the value of the Option if the stated condition is satisfied.
Example:// Scala program of the method// 'filter'// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){// Using Some classvalsome:Option[Int]=Some(30)// Applying filter methodvalx=some.filter(y=>{y%3==0})// Displays the value of the// option if the predicate// is satisfiedprintln(x)}}Output:Some(30)
Here, the condition is satisfied so, the Option value is returned and returns None if the predicate is not satisfied.
- def filterNot(p: (A) => Boolean): Option[A]
This method will return the Option value if the stated condition is not satisfied.
Example:// Scala program of the method// 'filterNot'// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){// Using Some classvalsome:Option[Int]=Some(30)// Applying filterNot methodvalx=some.filterNot(y=>{y%3!=0})// Displays the value of the// option if the predicate// is not satisfiedprintln(x)}}Output:Some(30)
Here, the condition is not satisfied so, the Option value is returned and returns None if the predicate is satisfied.
- def isDefined: Boolean
This method returns true if the Option is an instance of Some and returns false if the Option is an instance of None.
Example:// Scala program of the method// 'isDefined'// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){// Using Some classvalsome:Option[Int]=Some(30)// using None classvalnone:Option[Int]=None// Applying isDefined methodvalx=some.isDefinedvaly=none.isDefined// Displays true for Some// and false for Noneprintln(x)println(y)}}Output:true false
- def iterator: Iterator[A]
This method returns an iterator on the Option given.
Example:// Scala program of the method// 'iterator'// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){// Using Some classvalsome:Option[Int]=Some(30)// Applying iterator methodvalx=some.iterator// Displays an iteratorprintln(x)}}Output:non-empty iterator
- def map[B](f: (A) => B): Option[B]
This method will return the value of the function stated in the Map, if the Option has a value.
Example:// Scala program of the method// 'map'// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){// Using Some classvalsome:Option[Int]=Some(30)// Applying Map methodvalx=some.map(y=>{y + y})// Displays the value returned// by the function in mapprintln(x)}}Output:Some(60)
These were the methods to call on an Option and there are more such methods.
- def orElse[B >: A](alternative: => Option[B]): Option[B]
If the Option contain a value, this returns it. Otherwise, this method evaluates the alternative and returns alternative.
- def orNull
This method will return Null, if the Option didn’t contain a value.


