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 6120337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:39:25+00:00 2026-05-23T15:39:25+00:00

I recently stumbled over this post , which introduces the collect method for Scala

  • 0

I recently stumbled over this post, which “introduces” the collect method for Scala collections. The usage is straight forward:

scala> val ints = List(1, "2", 3) collect { case i: Int => i }
ints: List[Int] = List(1, 3)

Now maps are basically lists of key-value pairs, which are represented by tuples in Scala. So you might wanna try something like this:

scala> val pairs = Map(1 -> "I", "II" -> 2)
pairs: scala.collection.immutable.Map[Any,Any] = Map(1 -> I, II -> 2)

scala> val intsToStrings = pairs collect { case pair: (Int, String) => pair }

The compiler complains of course due to the type erasure model of the JVM, so the first thing we try is using existential types:

scala> val intsToStrings = pairs collect { case pair: (_, _) => pair }
intsToString: scala.collection.immutable.Map[Any,Any] = Map(1 -> I, II -> 2)

Although the code passed the compiler, and the result is “correct” (we wanted pairs => we got pairs) we still didn’t get what we actually wanted. The second attempt looks like this:

scala> val intsToStrings = pairs collect {
     |    case pair: (_, _) if pair._1.isInstanceOf[Int] && pair._2.isInstanceOf[String] => pair
     | }
intsToStrings: scala.collection.immutable.Map[Any,Any] = Map(1 -> I)

Ok, we are almost there:

scala> val realIntsToRealStrings = intsToStrings map {
     |    pair => (pair._1.asInstanceOf[Int], pair._2.asInstanceOf[String])
     | }
realIntsToRealStrings: scala.collection.immutable.Map[Int,String] = Map(1 -> I)

We did it, but instead of only casting from (Any,Any) to (Int,String) we actually copied each pair and thus created a new pair.

Now comes the question part. As I mentioned “The compiler complains of course…” I made it sound like I really know what I’m talking about. I don’t! All I know is that Java didn’t have generics from the beginning. At some point generics came into Java but not into the JVM. So the compiler checks all the types, but as soon as the code is running, JVM does not care for the parametric type. It only sees that it’s a Map or a List but not that it’s a Map[String, Int] or List[Int].

So here is my question.

With all the checking, casting and mapping, we managed to transfer a Map[Any,Any] to Map[String,Int]. Is there a better way of doing that? I mean the types are there, JVM just does not see them (as far as I am concerned)…

  • 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-23T15:39:25+00:00Added an answer on May 23, 2026 at 3:39 pm
    pairs collect { case p @ (_: Int, _: String) => p.asInstanceOf[(Int, String)] }
    

    or more concise, but with some overhead, I think

    pairs collect { case (x: Int, y: String) => (x, y) }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I stumbled over code such as this: void foo(const Bar* b) { ...
Recently I stumbled over this code snippet in Ruby: @data = 3.chr * 5
I recently stumbled across this entry in the google testing blog about guidelines for
I recently stumbled over a problem caused by some very old code I wrote
I have recently stumbled over a seeming inconsistency in Python's way of dealing with
Recently I stumbled across this pretty slick JS library called nodeJS that acts like
I'm new to this site having stumbled across it recently (and am loving it!)
Recently I've stumbled over surface. It's incredible and so easy to develop small applications
I'm currently using URL Rewriting, but I recently stumbled upon this link on Rerouting
I recently stumbled upon the Object.create() method in JavaScript, and am trying to deduce

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.