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

  • Home
  • SEARCH
  • 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 6322567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:21:22+00:00 2026-05-24T16:21:22+00:00

My code is as follows import scala.collection.mutable.HashMap type CrossingInterval = (Date, Date) val crossingMap

  • 0

My code is as follows

import scala.collection.mutable.HashMap
type CrossingInterval = (Date, Date)
val crossingMap = new HashMap[String, CrossingInterval]
val crossingData: String = ...

Firstly why does the following line compile?

val time = crossingMap.getOrElse(crossingData, -1)

I would have thought -1 would have been an invalid value

Secondly how do I do a basic check such as the following

if (value exists in map) {
}
else {
}

In Java I would just check for null values. I’m not sure about the proper way to do it in Scala

  • 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-05-24T16:21:23+00:00Added an answer on May 24, 2026 at 4:21 pm

    Typing your code in the interpreter shows why the first statement compiles:

    type Date = String
    scala> val time = crossingMap.getOrElse(crossingData, -1)
    time: Any = -1
    

    Basically, getOrElse on a Map[A, B] (here B = CrossingDate) accepts a parameter of any type B1 >: B: that means that B1 must be a supertype of B. Here B1 = Any, and -1 is of course a valid value of type Any. In this case you actually want to have a type declaration for time.

    For testing whether a key belongs to the map, just call the contains method. An example is below – since Date was not available, I simply defined it as an alias to String.

    scala> crossingMap.contains(crossingData)
    res13: Boolean = false
    scala> crossingMap += "" -> ("", "")
    res14: crossingMap.type = Map("" -> ("",""))
    //Now "" is a map of the key
    scala> crossingMap.contains("")
    res15: Boolean = true
    

    If you want to check whether a value is part of the map, the simplest way is to write this code:

    crossingMap.values.toSet.contains("")
    

    However, this builds a Set containing all values. EDIT: You can find a better solution for this subproblem in Kipton Barros comment.

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

Sidebar

Related Questions

I have the following Scala code. import scala.actors.Actor object Alice extends Actor { this.start
I have some code that is structured as follows from my.modules import MyClass Class
What advantage, if any, is provided by formatting C code as follows: while(lock_file(lockdir)==0) {
So, simple procedure, calculate a factorial number. Code is as follows. int calcFactorial(int num)
I've written a rails app that follows the regular directory structure (model code in
Ok, here is the code and then the discussion follows: public class FlatArrayList {
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
Working through a sample in Chapter 3 of Programming in Scala, the following code
I defined an annotation as follows import java.lang.annotation.ElementType import java.lang.annotation.Inherited import java.lang.annotation.Retention import java.lang.annotation.RetentionPolicy
I have a JSON as follows { columns : [RULE_ID,COUNTRY_CODE], RULE_ID : [1,2,3,7,9,101,102,103,104,105,106,4,5,100,30], COUNTRY_CODE

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.