Open In App

Scala | Methods to Call Option

Last Updated : 17 Apr, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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.


Similar Reads

Scala | Methods to Call on a Map | Set-1
Prerequisite- Scala Map. In Scala there are foremost Methods to call on a Map. A Scala Method is a section of a class which has a designation, a signature, optionally a few annotations, and whatsoever byte-code. A function which is interpreted as a member of some Object is termed as a Method and the Map Method is exactly incorporated with Collectio
11 min read
Methods to call on a Set in Scala
A set is a collection that only contains unique items. In Scala, both mutable and immutable sets are available. The mutable set is those set in which the value of the object is change, but, in the immutable set, the value of the object is not changed itself. The immutable set is defined under Scala.collection.immutable._ package and mutable set are
13 min read
Scala | Functions Call-by-Name
In Scala when arguments pass through call-by-value function it compute the passed-in expression's or arguments value once before calling the function . But a call-by-Name function in Scala calls the expression and recompute the passed-in expression's value every time it get accessed inside the function. Here example are shown with difference and sy
3 min read
Call a method on a Super Class in Scala
This concept is used when we want to call super class method. So whenever a base and subclass have same named methods then to resolve ambiguity we use super keyword to call base class method. The keyword "super" came into this with the concept of Inheritance. Below is the example of call a method on a superclass. Example #1: // Scala program to cal
2 min read
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 i
3 min read
Scala - String Methods with Examples
In Scala, as in Java, a string is a sequence of characters. In Scala, objects of String are immutable which means a constant and cannot be changed once created. In the rest of this section, we discuss the important methods of java.lang.String class. char charAt(int index): This method is used to returns the character at the given index. Example: C/
12 min read
How to Qualify Methods as Static in Scala?
In this article, we will learn how to qualify methods as static in Scala. you declare a method as static using the object keyword instead of the class keyword. By defining a method within an object, you create a singleton object that can hold static methods. Table of Content Using Object ApproachUsing Companion Object ApproachSyntax: object MyClass
2 min read
Scala short <(x: Short): Boolean
Short, a 16-bit signed integer (equivalent to Java's short primitive type) is a subtype of scala.AnyVal. The <(x: Short) method is utilized to return true if this value is less than x, false otherwise. Method Definition: def <(x: Short): Boolean Return Type: It returns true if this value is less than x, otherwise false. Example #1: // Scala p
1 min read
Scala short <(x: Char): Boolean
Short, a 16-bit signed integer (equivalent to Java's short primitive type) is a subtype of scala.AnyVal. The <(x: Char) method is utilized to return true if this value is less than x, false otherwise. Method Definition: def <(x: Char): BooleanReturn Type: It returns true if this value is less than x, otherwise false. Example #1: C/C++ Code //
1 min read
Scala Extractors
In Scala Extractor is defined as an object which has a method named unapply as one of its part. This method extracts an object and returns back the attributes. This method is also used in Pattern matching and Partial functions. Extractors also explains apply method, which takes the arguments and constructs an object so, it's helpful in constructing
6 min read