Scala - NULL handling with MAP

Sharing three useful types that express a very useful concept i learned today, for NULL handling.

Most languages have a special keyword or type instance that’s assigned to reference variables when there’s nothing else for them to refer to. In Java, it’s null, which is a keyword, not an instance of a type. Thus, it’s illegal to call any methods on it. But this is a confusing choice on the language designer’s part. Why return a keyword when the programmer expects an instance of a type? 

Of course, the real problem is that null is a giant source of nasty bugs. What null really signals is that we don’t have a value in a given situation. If the value is not null, we do have a value. Why not express this situation explicitly with the type system and exploit type checking to avoid NullPointerExceptions? 

Option - lets us express this situation explicitly without the null “hack.” Option is an abstract class and its two concrete subclasses are 

  1. Some, for when we have a value, and 
  2. None, when we don’t. 

If the Option is a Some, Some.get returns the value. However, if the Option is actually None, then None.get throws a NoSuchElementException, in this case need to handle with safer alternative getOrElse

In action with Scala worksheet

val stateCapitals = Map( 

"Alabama" -> "Montgomery", 

"Alaska" -> "Juneau", 

"Wyoming" -> "Cheyenne") 

println( "Get the capitals wrapped in Options:" ) 

println( "Alabama: " + stateCapitals.get("Alabama") ) 

println( "Wyoming: " + stateCapitals.get("Wyoming") ) 

println( "Unknown: " + stateCapitals.get("Unknown") ) 

println( "Get the capitals themselves out of the Options:" ) 

println( "Alabama: " + stateCapitals.get("Alabama").get ) 

println( "Wyoming: " + stateCapitals.get("Wyoming").getOrElse("Oops!") ) 

println( "Unknown: " + stateCapitals.get("Unknown").getOrElse("Oops2!") ) 

Output

Get the capitals wrapped in Options:

Alabama: Some(Montgomery)

Wyoming: Some(Cheyenne)

Unknown: None

Get the capitals themselves out of the Options:

Alabama: Montgomery

Wyoming: Cheyenne

Unknown: Oops2!

要查看或添加评论,请登录

Divagar Carlmarx的更多文章

  • Processing large amount of CSV data using JAVA

    Processing large amount of CSV data using JAVA

    Have you worked with large amount of csv DATA in GBs ?? And you have memory constraints ?? This might help for you…

    1 条评论
  • Fell in love with Scala

    Fell in love with Scala

    I was a hard core JAVA developer in both my professional and learning journey, but recently for a reason i have started…

  • Scala - Sealed Class Hierarchies

    Scala - Sealed Class Hierarchies

    In my previous article i had shared you regarding Option feature in Scala, in this article come lets discuss about…

  • WHY and HOW I started using IntelliJ IDE and SCALA

    WHY and HOW I started using IntelliJ IDE and SCALA

    I was using Eclipse IDE for java enterprise development from beginning of my career and learning journey. In my life…

  • Product based company team management strategies for productivity

    Product based company team management strategies for productivity

    I am sharing my knowledge i got in my professional and personal life as software developer for team management. Lets…

  • Big Data Volume

    Big Data Volume

    Big Data Volume Data volume is characterized by the amount of data that is generated continuously. Different data types…

    2 条评论
  • Distributed Systems - Multi Leader Replication

    Distributed Systems - Multi Leader Replication

    We know in Leader follower model, client can able to write only by leader this if leader is down for any reason, you…

  • Distributed Systems - Replication

    Distributed Systems - Replication

    Replication means keeping a copy of the same data on multiple machines that are connected via a network. Reasons for…

  • Transaction Processing or Analytics ?

    Transaction Processing or Analytics ?

    Transaction processing systems In the early days of business data processing, a write to the database typically…

  • Designing key value database with btree

    Designing key value database with btree

    Introduced in 1970 and called “ubiquitous” less than 10 years later , B-trees have stood the test of time very well…

社区洞察

其他会员也浏览了