Scala | Methods to Call Option Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 1 Likes Like 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 // 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 // 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 // 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 // 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 // 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 // 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 // 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 // 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 // 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. Comment N nidhi1352singh Follow 1 Improve N nidhi1352singh Follow 1 Improve Article Tags : Scala Scala Scala-Method scala-collection Explore OverviewScala Programming Language3 min readIntroduction to Scala7 min readSetting up the environment in Scala3 min readHello World in Scala2 min readBasicsScala Keywords2 min readScala Identifiers3 min readData Types in Scala3 min readVariables in Scala3 min readControl StatementsScala | Decision Making (if, if-else, Nested if-else, if-else if)5 min readScala | Loops(while, do..while, for, nested loops)5 min readBreak statement in Scala3 min readScala | Literals4 min readOOP ConceptsClass and Object in Scala5 min readInheritance in Scala5 min readOperators in Scala11 min readScala Singleton and Companion Objects3 min readScala Constructors4 min readScala | Polymorphism5 min readScala | Multithreading3 min readScala this keyword2 min readMethodsScala | Functions - Basics3 min readAnonymous Functions in Scala2 min readScala | Closures3 min readRecursion in Scala4 min readMethod Overloading in Scala5 min readMethod Overriding in Scala8 min readLambda Expression in Scala4 min readScala Varargs2 min readStringsScala String4 min readScala | String Interpolation3 min readScala | StringContext2 min readRegular Expressions in Scala5 min readStringBuilder in Scala4 min readScala PackagesPackages In Scala4 min readScala | Package Objects3 min readChained Package Clauses in Scala3 min readFile Handling in Scala3 min readScala TraitScala | Traits7 min readScala | Sealed Trait4 min readScala | Trait Mixins3 min readTrait Linearization in Scala5 min readCollectionsScala Lists5 min readScala ListBuffer6 min readListSet in Scala6 min readScala Map5 min readScala | Arrays6 min readScala | ArrayBuffer4 min readScala | Tuple5 min readSet in Scala | Set-13 min readSet in Scala | Set-27 min readBitSet in Scala5 min readHashSet In Scala4 min readStack in Scala3 min readHashMap in Scala3 min readTreeSet in Scala4 min readIterators in Scala5 min readScala | Option3 min read Like