{"id":1626,"date":"2014-06-04T11:33:34","date_gmt":"2014-06-04T10:33:34","guid":{"rendered":"http:\/\/www.xenonique.co.uk\/blog\/?p=1626"},"modified":"2014-06-04T11:33:34","modified_gmt":"2014-06-04T10:33:34","slug":"scala-maps-and-sorting","status":"publish","type":"post","link":"https:\/\/www.xenonique.co.uk\/blog\/2014\/06\/04\/scala-maps-and-sorting\/","title":{"rendered":"Scala Maps and Sorting"},"content":{"rendered":"<p>This is blog entry originally written in the middle May 2014. I also seem to writing this code every 3 months or so and then I forgot. So I need a trigger to remember exactly how to work Scala Maps and sort the entries by key.<br \/>\nLet&#8217;s break out the Scala REPL.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nWelcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_51).\r\nType in expressions to have them evaluated.\r\nType :help for more information.\r\n<\/pre>\n<p>We will create a map of collection of String associated with Integer elements. This data might have come a key value storage or you may have a similar requirement to work with statistical data.<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\r\nscala&gt; val map = Map( &quot;Jane&quot; -&gt; 3, &quot;Peter&quot; -&gt; 10, &quot;Steve&quot; -&gt; 1, &quot;Anna&quot; -&gt; 5, &quot;Megan&quot; -&gt; 15, &quot;Brian&quot; -&gt; 7, &quot;Sally&quot; -&gt;  8 )\r\nmap: scala.collection.immutable.Map[String,Int] = Map(Megan -&gt; 15, Anna -&gt; 5, Jane -&gt; 3, Brian -&gt; 7, Steve -&gt; 1, Sally -&gt; 8, Peter -&gt; 10)\r\n<\/pre>\n<p>In order to sort the data structure, we then convert the map to list collection of tuples. <\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\r\nscala&gt; map.toList\r\nres1: List[(String, Int)] = List((Megan,15), (Anna,5), (Jane,3), (Brian,7), (Steve,1), (Sally,8), (Peter,10))\r\n<\/pre>\n<p>Now we can sort using the tuples. Here is the descending order on the values:<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\r\nscala&gt; map.toList.sortWith( (x,y) =&gt; x._2 &gt; y._2 )\r\nres2: List[(String, Int)] = List((Megan,15), (Peter,10), (Sally,8), (Brian,7), (Anna,5), (Jane,3), (Steve,1))\r\n<\/pre>\n<p>Here is the ascending order on the values<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\r\nscala&gt; map.toList.sortWith( (x,y) =&gt; x._2 &lt; y._2 )\r\nres3: List[(String, Int)] = List((Steve,1), (Jane,3), (Anna,5), (Brian,7), (Sally,8), (Peter,10), (Megan,15))\r\n<\/pre>\n<p>Here is another tip. Let&#8217;s say you need to create a histogram of 100 different product items. You can create this data structure<br \/>\nimmutable at the beginning. However, as you are building the statistic, you may prefer to use an mutable collection map instead.<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\r\n    val buckets = collection.mutable.Map.empty[Int,Int]\r\n    buckets ++= (1 to 100).toList.map{ x =&gt; (x,0) }.toMap\r\n<\/pre>\n<p>Now in your statistic gathering part, you can write something like this:<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\r\n    val keyName: String = ???\r\n    val keyIndex: Integer = convertNameToIndex(keyName)\r\n    buckets(keyIndex) =  buckets.getOrElse(keyIndex, 0) + 1\r\n<\/pre>\n<p>And then render the sorted data into a top ten products<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\r\n val sortedProducts = buckets.toList.sortWith{ (x,y) =&gt; x._2 &gt; y._2 }\r\n println(sortedProducts.take(10))\r\n<\/pre>\n<p>That&#8217;s all.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is blog entry originally written in the middle May 2014. I also seem to writing this code every 3 months or so and then I forgot. So I need a trigger to remember exactly how to work Scala Maps and sort the entries by key. Let&#8217;s break out the Scala REPL. We will create [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[75,40,6],"tags":[],"_links":{"self":[{"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/posts\/1626"}],"collection":[{"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=1626"}],"version-history":[{"count":4,"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/posts\/1626\/revisions"}],"predecessor-version":[{"id":1635,"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/posts\/1626\/revisions\/1635"}],"wp:attachment":[{"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=1626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=1626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=1626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}