Scala | Option Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article 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. 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 // 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) } } Output: 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 // Scala program for Option // with Pattern matching // Creating object object pattern { // Main method def main(args: Array[String]) { // Creating a Map val name = Map("Nidhi" -> "author", "Geeta" -> "coder") //Accessing keys of the map println(patrn(name.get("Nidhi"))) println(patrn(name.get("Rahul"))) } // Using Option with Pattern // matching def patrn(z: Option[String]) = z match { // for 'Some' class the key for // the given value is displayed case Some(s) => (s) // for 'None' class the below string // is displayed case None => ("key not found") } } Output: 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 // Scala program of using // getOrElse method // Creating object object get { // Main method def main(args: Array[String]) { // Using Some class val some:Option[Int] = Some(15) // Using None class val none:Option[Int] = None // Applying getOrElse method val x = some.getOrElse(0) val y = none.getOrElse(17) // Displays the key in the // class Some println(x) // Displays default value println(y) } } Output: 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 // Scala program of using // isEmpty method // Creating object object check { // Main method def main(args: Array[String]) { // Using Some class val some:Option[Int] = Some(20) // Using None class val none:Option[Int] = None // Applying isEmpty method val x = some.isEmpty val y = none.isEmpty // Displays true if there // is a value else false println(x) println(y) } } Output: false true Here, isEmpty returns false for Some class as it non-empty but returns true for None as its empty. Comment More info N nidhi1352singh Follow Improve Article Tags : Scala Scala 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