Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9014863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:36:54+00:00 2026-06-16T03:36:54+00:00

As a Java-to-Scala switcher, I routinely find myself rewriting null handling stuff like val

  • 0

As a Java-to-Scala switcher, I routinely find myself rewriting null handling stuff like

val itemOpt: Option[Item] = items.get(coords) // "items" is something like a Map
if (itemOpt.isDefined) {
  val item = itemOpt.get
  // do something with item, querying item fields a lot of times, for example
  if (item.qty > 10) {
    storeInVault(item.name, item.qty, coords)
  } else {
    storeInRoom(item)
  }
}

I guess it looks ugly and it really looks like a piece of code rewritten from Java’s:

Item item = items.get(coords);
if (item != null) {
  // do something with item, querying item fields a lot of times, for example
}

It also looks ugly in Java, but at least it’s one line less. What is the best practice to handle such simple cases in Scala? I already know of flatMap and flatten to handle collections of Option[Stuff], and I know of getOrElse to handle default values. I dream of something like:

items.get(coords).doIfDefined(item =>
  // do stuff with item
)

but I don’t see anything like that in Option API.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:36:55+00:00Added an answer on June 16, 2026 at 3:36 am

    Very popular usage pattern:

    val item: Option[Int] = None
    val result = item map (_ + 1) getOrElse 0
    

    so you just use map in order to transform value if it’s defined.

    If you just want to use value, that is stored within an Option, then just use foreach:

    item foreach { it =>
        println(it)
    }
    

    As you can see, Option also supports many collection methods, so you actually don’t need to learn new API. You can just treat it as a collection with either 1 or 0 elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When working with Java from Scala, we have to account for null. HttpServletRequest getters
Will Java 8 support pattern matching like Scala and other functional programs do? I'm
I have an existing java/scala application using a global thread pool. I would like
I've recently switched my assignment from Java to Scala. However, it still looks like
In the paper Loop Recognition in C++/Java/Go/Scala (pdf) we find the following quote in
I would like to know that if java/scala has the string object that could
I'm migrating an app from java to Scala. In java I have somethng like
I'm a Scala/Java programmer looking to reintroduce myself to C++ and learn some of
I have an application where I would like to have mixed Java and Scala
I am trying to compile a Maven Java/Scala mixed project that has a Scala

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.