Skip to content
Related Articles

Related Articles

Scala | Methods to Call Option
  • Last Updated : 17 Apr, 2019

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 object
    object GFG
    {
      
        // Main method
        def main(args: Array[String])
        {
      
            // Using Some class
            val some:Option[Int] = Some(20)
          
            // Applying get method
            val x = some.get
      
            // Displays the value 
            println(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 object
    object GFG
    {
      
        // Main method
        def main(args: Array[String])
        {
      
            // Using Some class
            val some:Option[Int] = Some(20)
          
            // Applying productArity
            // method
            val x = some.productArity
      
            // Displays the size of
            // the Option's value
            println(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 object
    object GFG
    {
      
        // Main method
        def main(args: Array[String])
        {
      
            // Using Some class
            val some:Option[Int] = Some(20)
      
            // Applying productElement
            // method
            val x = some.productElement(0)
      
            // Displays the element
            println(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 object
    object GFG
    {
      
        // Main method
        def main(args: Array[String])
        {
      
            // Using Some class
            val some:Option[Int] = Some(30)
      
            // Applying exists method
            val x = some.exists(y => {y % 3 == 0})
      
            // Displays true if the condition
            // given is satisfied else false 
            println(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 object
    object GFG
    {
      
        // Main method
        def main(args: Array[String])
        {
      
            // Using Some class
            val some:Option[Int] = Some(30)
      
            // Applying filter method
            val x = some.filter(y => {y % 3 == 0})
      
            // Displays the value of the
            // option if the predicate 
            // is satisfied
            println(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 object
    object GFG
    {
      
        // Main method
        def main(args: Array[String])
        {
      
            // Using Some class
            val some:Option[Int] = Some(30)
      
            // Applying filterNot method
            val x = some.filterNot(y => {y % 3 != 0})
      
            // Displays the value of the
            // option if the predicate 
            // is not satisfied
            println(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 object
    object GFG
    {
      
        // Main method
        def main(args: Array[String])
        {
      
            // Using Some class
            val some:Option[Int] = Some(30)
      
            // using None class
            val none:Option[Int] = None
      
            // Applying isDefined method
            val x = some.isDefined
            val y = none.isDefined
      
            // Displays true for Some
            // and false for None
            println(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 object
    object GFG
    {
      
        // Main method
        def main(args: Array[String])
        {
      
            // Using Some class
            val some:Option[Int] = Some(30)
          
            // Applying iterator method
            val x = some.iterator
      
            // Displays an iterator
            println(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 object
    object GFG
    {
      
        // Main method
        def main(args: Array[String])
        {
      
            // Using Some class
            val some:Option[Int] = Some(30)
      
            // Applying Map method
            val x = some.map(y => {y + y})
      
            // Displays the value returned
            // by the function in map
            println(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.
My Personal Notes arrow_drop_up
Recommended Articles
Page :