Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Map[String,String] from Map[String, Any] in Scala?

Tags:

map

scala

any

New to scala - How to create a Map[String,String] from Map[String, Any] The values of the Map[String,Any] are strings but I don't know how to cast or otherwise coerce the "Any" type to a "String" type.

like image 784
coolgar Avatar asked Oct 09 '14 04:10

coolgar


People also ask

What is map string string in Scala?

A map represents an mapping from keys to values. The Scala Map type has two type parameters, for the key type and for the value type. So a Map[String, Int] maps strings to integers, while a Map[Int, Set[String]] maps integers to sets of strings. Scala provides both immutable and mutable maps.

How do you turn a map into a string?

Use Object#toString() . String string = map. toString();

How do I make a map map in Scala?

Map class explicitly. If you want to use both mutable and immutable Maps in the same, then you can continue to refer to the immutable Map as Map but you can refer to the mutable set as mutable. Map. While defining empty map, the type annotation is necessary as the system needs to assign a concrete type to variable.

What is the syntax of creating map in Scala?

// Creation of Map having key-value. // pairs of type (String, Int) val mapMut = scala.collection.mutable.Map[String, Int]() println( "Empty Map: " + mapMut)


2 Answers

As you mentioned that all the values in your map are strings, you can simply use asInstanceOf. If your assumption is incorrect, you will receive runtime exceptions as demonstrated below:

$ scala
Welcome to Scala version 2.9.2 (OpenJDK 64-Bit Server VM, Java 1.7.0_55).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val m:Map[String, Any] = Map("foo" -> 5, "bar" -> 7.6, "baz" -> "qux")
m: Map[String,Any] = Map(foo -> 5, bar -> 7.6, baz -> qux)

scala> val m2: Map[String, Any] = Map("foo" -> "5", "bar" -> "7.6", "baz" -> "qux")
m2: Map[String,Any] = Map(foo -> 5, bar -> 7.6, baz -> qux)

scala> m2.asInstanceOf[Map[String, String]]
res0: Map[String,String] = Map(foo -> 5, bar -> 7.6, baz -> qux)

This is perfect when all values are actually of type String.

scala> res0("foo")
res5: String = 5

Watch out for your wrong assumption:

scala> m.asInstanceOf[Map[String, String]]
res2: Map[String,String] = Map(foo -> 5, bar -> 7.6, baz -> qux)

scala> res2("foo")
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
    at .<init>(<console>:10)
    at .<clinit>(<console>)
    at .<init>(<console>:11)
    at .<clinit>(<console>)
    at $print(<console>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)
    at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920)
    at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43)
    at scala.tools.nsc.io.package$$anon$2.run(package.scala:25)
    at java.lang.Thread.run(Thread.java:744)
like image 101
tuxdna Avatar answered Oct 06 '22 18:10

tuxdna


Having a Map[String, Any] in the first place is a sign something has probably gone off the rails, although without any code to look at, I can't really help you there. Typically, when you start to see Anys and other super-generic types inferred, it's Scala's type system telling you that the code you've written does not mean what you think it means.

If you really want to do this, you can do:

scala> val m = Map("foo" -> 5, "bar" -> 7.6, "baz" -> "qux")
m: scala.collection.immutable.Map[String,Any] = Map(foo -> 5, bar -> 7.6, baz -> qux)

scala> m.mapValues(_.toString)
res0: scala.collection.immutable.Map[String,String] = Map(foo -> 5, bar -> 7.6, baz -> qux)
like image 3
acjay Avatar answered Oct 06 '22 17:10

acjay