Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Java LinkedHashMap to Scala LinkedHashMap?

I'm new to Scala. I've been trying to convert a java LinkedHashMap to an equivalent collection(LinkedHashMap?) in Scala in order to preserve the insertion order.

Tried following things as suggested in other threads, but nothing seems to work!

scalaAsMap() - is messing up the order

TreeMap() - sort on keys, values, etc. is not something I'm looking for

Explicit conversion is not working.

val f = new java.util.LinkedHashMap[String, java.util.Map[String, String]]

var g: scala.collection.mutable.LinkedHashMap[String, java.util.Map[String, String]] = f
like image 600
miniscule001 Avatar asked Aug 07 '26 10:08

miniscule001


1 Answers

Hmm, how about:

val javaMap = new java.util.LinkedHashMap[String, String]()
val scalaMap = javaMap.asScala

The type of scalaMap is Map[String, String] but under the hood it behaves just like LinkedHashMap.

like image 132
Porcupine Avatar answered Aug 09 '26 07:08

Porcupine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!