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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:49:55+00:00 2026-06-10T10:49:55+00:00

In the Scala shell I did this: import java.util._ import scala.collection.JavaConversions._ val t: SortedMap[String,Int]

  • 0

In the Scala shell I did this:

import java.util._
import scala.collection.JavaConversions._

val t: SortedMap[String,Int] = new TreeMap[String,Int] () // produces an empty java.util.SortedMap

t("a") = 1; t("b") = 2; t("A") = 3; t("0") = 4

t // prints: res35: java.util.SortedMap[String,Int] = {0=4, A=3, a=1, b=2}, as expected

t foreach println // also prints the (k,v) pairs in the same TreeMap sorted order

However, the following statement does not print the pairs in sorted order, it appears to print them in hash bucket order, (0,4) (b,2) (A,3) (a,1):

for ((k,v) <- t) printf("(%s,%d)%n", k, v)

In other answers related to for and foreach it seems that a for comprehension is supposed to translate into a use of foreach like so:

“A for-comprehension for (p <- e) e0 is translated to e.foreach { case p => e0 }”

but that does not seem to be what is happening here.

Note that if I create a scala SortedMap from a scala TreeMap that both the foreach and the for produce the (k,v) pairs in sorted order as I expected. It seems like some way that the Java TreeMap is being converted for scala is different.

Any comments or ideas about why this discrepancy?

  • 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-10T10:49:57+00:00Added an answer on June 10, 2026 at 10:49 am

    Well, p and (k,v) are not the same thing. Your for-comprehension is translated thus:

    t.filter{
      case (k, v) => true
      case _      => false
    }.map {
      case (k, v) => printf("(%s,%d)$n")
    }
    

    Once filter puts it’s hands on the java collection, it becomes a Scala collection, and is no longer sorted.

    As a side note, on Scala 2.10 the above is no longer true since it detects that there’s no element that isn’t a (k,v) from the static type at compile time, and doesn’t generate the filter. Here:

    scala> for ((k,v) <- t) printf("(%s,%d)%n", k, v)
    <console>:15: warning: dead code following this construct
                  for ((k,v) <- t) printf("(%s,%d)%n", k, v)
                             ^
    (0,4)
    (A,3)
    (a,1)
    (b,2)
    
    scala> import scala.reflect.runtime.universe._
    import scala.reflect.runtime.universe._
    
    scala> reify{for ((k,v) <- t) printf("(%s,%d)%n", k, v)}
    res4: reflect.runtime.universe.Expr[Unit] =
    Expr[Unit](scala.collection.JavaConversions.mapAsScalaMap(t).foreach(((x$1) => x$1: @unchecked match {
      case scala.Tuple2((k @ _), (v @ _)) => scala.this.Predef.printf("(%s,%d)%n", k, v)
    })))
    

    PS: Prefer JavaConverters over JavaConversions.

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

Sidebar

Related Questions

scala> import scala.collection.mutable.Buffer import scala.collection.mutable.Buffer scala> val b = Buffer(1, 2, 3) b: scala.collection.mutable.Buffer[Int]
scala> val m = Map(1 -> 2) m: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala>
scala> import java.util.Properties import java.util.Properties scala> trait Foo extends Properties defined trait Foo scala>
scala> class A (s: String*) { val l: ListBuffer[String] = ListBuffer[String](s) } <console>:8: error:
scala> Random.shuffle((1 to 10).toSet) res10: scala.collection.immutable.Set[Int] = Set(5, 10, 1, 6, 9, 2, 7,
scala> val l = List((1,2), (2,3)) l: List[(Int, Int)] = List((1,2), (2,3)) I can
scala 2.9.2 This compiles fine object AppBuilder extends App { def app( blockw: Int
scala.math.ScalaNumber is a Java file which looks like this: public abstract class ScalaNumber extends
Using Scala's command line REPL: def foo(x: Int): Unit = {} def foo(x: String):
I'm trying to replace a bunch of Linux shell scripts with Scala scripts. One

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.