PEAT Online Course -
How To Improve Your Personal Performance in Java work Technology

  • YOU + CONTROL
  • Focus
  • Autonomy
  • Proficiency
  • Purpose
  • Covid-19!!!
  • Deep Hidden Meaning
  • FREE updates
GET 75% OFF NOW
Your Promo code: TECHLAYOFF
Exclusively on
VIMEO ON-DEMAND

Custom Ordering Scala TreeMap

12 July 2012 1 comment

3 minutes

566

How do you get custom ordering in a Scala TreeMap?

Well this puzzled me for a while. The answer lies in the world of implicits and receiver type converters.

In a nut shell, a scala.collection.immutable.TreeMap is a SortedMap. If you look at the documentation for TreeMap, you will see it takes an Ordering[T] as an implicit argument.

Normally when you declare a TreeMap, say inline, it will use the default Ordering object like so:-

scala> val dtm = TreeMap( "a" -> 1, "bc" -> 2, "def" -> 3 )
dtm: scala.collection.immutable.TreeMap[java.lang.String,Int] = Map(a -> 1, bc -> 2, def -> 3)

If you want to change the ordering of the keys, for example, instead of the ascending order by String content, into, say, a descending order of strings length then you need an Ordering type.

scala> object VarNameOrdering extends Ordering[String] {
         def compare(a:String, b:String) = b.length compare a.length
       }
defined module VarNameOrdering

Now you can use the second argument list in an explicit fashion like this:

scala> val tm1 = TreeMap( "a" -> 1, "bc" -> 2, "def" -> 3 )( VarNameOrdering )
tm: scala.collection.immutable.TreeMap[java.lang.String,Int] = Map(def -> 3, bc -> 2, a -> 1)

We pass the object to the TreeMap, which is rather similiar to a Java Collection Comparator object without the boilerplate instantiation. The keys of the TreeMap are now ordered by String lengths. We add more elements and the map will stay ordered.

 val tm2 = tm1 + ( "food" -> 4 )
cala.collection.immutable.TreeMap[java.lang.String,Int] = Map(food -> 4, def -> 3, bc -> 2, a -> 1)

However, a word of caution, one needs to be careful and remember that maps are usually implemented as hashes.

scala> val tm3 = tm2 + ( "z" -> 5 )
tm3: scala.collection.immutable.TreeMap[java.lang.String,Int] = Map(food -> 4, def -> 3, bc -> 2, z -> 5)

Surprised? You should be.

Another way to sort a map is just to get access to the keys and sort.

scala> dtm.keys.toList.sortWith ( _.length > _.length )
res3: List[java.lang.String] = List(salad, def, bc, a)

scala> dtm.keys.toList.sortWith ( _.length > _.length ).map( k => ( dtm.get(k).get ))
res4: List[Int] = List(10, 3, 2, 1)

scala> dtm.keys.toList.sortWith ( _.length > _.length ).map( k => ( k, dtm.get(k).get ))
res5: List[(java.lang.String, Int)] = List((salad,10), (def,3), (bc,2), (a,1))

This may well be a better solution, as you have not lost a key in flight! Considering how data is going to be stored is a major decision that needs to be taken early. You can always decide how to write projection of that data much later.

Finally, it is interesting to see the parallels between Java and Scala

scala> dtm.keys
res6: Iterable[java.lang.String] = Set(a, bc, def, salad)

scala> dtm.keys.toList
res7: List[java.lang.String] = List(a, bc, def, salad)

PS: I can safely say I have used Scala professionally today in financial enterprise.

1 Comment

  1. scala> dtm.keys.toList.sortWith ( _.length > _.length ).map( k => ( dtm.get(k).get ))
    res4: List[Int] = List(10, 3, 2, 1)

    scala> dtm.keys.toList.sortWith ( _.length > _.length ).map( k => ( k, dtm.get(k).get ))
    res5: List[(java.lang.String, Int)] = List((salad,10), (def,3), (bc,2), (a,1))

    can just by:

    scala> dtm.toList.sortBy(-_._1.size)
    res: List[(String, Int)] = List((salad,10), (def,3), (bc,2), (a,1))

    Comment by zz — 24 July 2012 @ 3:00 am

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Hey all! Thanks for visiting. I provide fringe benefits to interested readers: checkout consultancy, training or mentorship Please make enquiries by email or
call +44 (0)7397 067 658.

Due to the Off-Payroll Working plan for the UK government, I am enforcing stricter measures on contracts. All potential public sector GOV.UK contracts engagements must be approved by QDOS and/or SJD Accounting. Please enquire for further information.