Scala Maps and Sorting

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’s break out the Scala REPL.
 
 
 
 
 
 
 

Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_51).
Type in expressions to have them evaluated.
Type :help for more information.

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.

scala> val map = Map( "Jane" -> 3, "Peter" -> 10, "Steve" -> 1, "Anna" -> 5, "Megan" -> 15, "Brian" -> 7, "Sally" ->  8 )
map: scala.collection.immutable.Map[String,Int] = Map(Megan -> 15, Anna -> 5, Jane -> 3, Brian -> 7, Steve -> 1, Sally -> 8, Peter -> 10)

In order to sort the data structure, we then convert the map to list collection of tuples.

scala> map.toList
res1: List[(String, Int)] = List((Megan,15), (Anna,5), (Jane,3), (Brian,7), (Steve,1), (Sally,8), (Peter,10))

Now we can sort using the tuples. Here is the descending order on the values:

scala> map.toList.sortWith( (x,y) => x._2 > y._2 )
res2: List[(String, Int)] = List((Megan,15), (Peter,10), (Sally,8), (Brian,7), (Anna,5), (Jane,3), (Steve,1))

Here is the ascending order on the values

scala> map.toList.sortWith( (x,y) => x._2 < y._2 )
res3: List[(String, Int)] = List((Steve,1), (Jane,3), (Anna,5), (Brian,7), (Sally,8), (Peter,10), (Megan,15))

Here is another tip. Let’s say you need to create a histogram of 100 different product items. You can create this data structure
immutable at the beginning. However, as you are building the statistic, you may prefer to use an mutable collection map instead.

val buckets = collection.mutable.Map.empty[Int,Int]
    buckets ++= (1 to 100).toList.map{ x => (x,0) }.toMap

Now in your statistic gathering part, you can write something like this:

val keyName: String = ???
    val keyIndex: Integer = convertNameToIndex(keyName)
    buckets(keyIndex) =  buckets.getOrElse(keyIndex, 0) + 1

And then render the sorted data into a top ten products

val sortedProducts = buckets.toList.sortWith{ (x,y) => x._2 > y._2 }
 println(sortedProducts.take(10))

That’s all.

Reference: Scala Maps and Sorting from our JCG partner Peter Pilgrim at the Peter Pilgrim’s blog blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


five × = 40



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close