Skip to main content

Scala: Map of Lists

Every developer has had to deal with a Map whose key is of type X and whose value is a list. Additions to the map often happen like this in Java:

List l = map.get(key);
if ( l == null ) {
l = new ArrayList();
map.put(key, l);

Kind of cumbersome, and kind of not thread-safe.

What is to be done?

Well, google-collections tries to take care of the cumbersome-ness with their MultiMap class. The put looks the same as in java.util.Map, but the get returns a List instead.

So, instead of the code above, you get:

map.put(key, value);

Now, if you want thread-safety as well, you call MultiMaps.synchronizedMap, passing your existing MultiMap.

So, I had a specific case that I was working on which was basically a group by function given a list of C. My decision was to create in the end a map with an A for the key and a List of B for the value where A is the type of the group by property and B a projection of C (So, the List could be of C as well).

Here is what I came up with:

def inclAsList[A,B,C](keyresolver:C=>A, valueresolver:A=>List[B], list:List[C]) = {
(list map { el => keyresolver(el) } removeDuplicates)
map { key => (key, valueresolver(key)) }
: _*

So, this is thread-safe. What it does is takes the list of C and applies a function to find the group by properties. Then, iterating through those, it applies a function with each key to get a list of values that match.

"But what about 'put'?" you say. The thing is, once you have immutability built in, you will find that the need to be able to modify a map doesn't come up as often. This example is prime--because Maps and Lists are immutable in Scala, we are nearly there before we know it regarding thread safety.


Popular posts from this blog

How Many Teeth Does The Tooth Fairy Pick Up Each Night in Utah?

Somebody asked me a question about my Tooth Fairy post the other day that got me thinking. How many baby teeth are lost every day in Utah?

I began with Googling. Surely someone else has thought of this and run some numbers, right? Lo, there is a tooth fairy site that claims that the Tooth Fairy collects 300,000 teeth per night.

That's a lot; however, when I ran the numbers, it started to seem awfully low.

Let's assume that the Tooth Fairy collects all baby teeth regardless of quality and we assume that all children lose all their baby teeth. The world population of children sits at 2.2 billion, with 74.2 million of them in the United States. Of those, approximately 896,961 of them are in Utah. This means that somewhere around .04077% of the world's children are in Utah.

If we assume that kids in Utah lose teeth at the same rate as all other children in the world and that each day in the year is just as likely as the rest to lose a tooth, then we have that of the alleged …

Five Reasons Serving on the Athlos Board is Fun Right Now

About 18 months ago, a friend of mine, Bethany Zeyer, let me know about an open position on the Athlos Academy of Utah school board. I've always had a passion for education, and my kids' school seemed like a place where I could have a positive effect on the community.

Also, I'd just finished reading "The Smartest Kids in the World" by Amanda Ripley and, based on Amanda's advice, interviewed the school's director.

I was in the mood to contribute!

I applied and was accepted, and I've been serving on the board for a little over a year now.

Since then, I've learned a whole lot about how a school is run.

I've learned that someone needs to determine the school guidelines for pesticide usage.

And that someone needs to be thinking about the long-term future of the school, whether or not to increase grade capacity, whether or not to match the pay increases big school districts are giving, and most importantly, evaluate whether or not the school is achi…

Twas the Night Before Pi Day

Twas the Night Before Pi Day
by Joshua Cummings

Twas the night before Pi Day, when Archimedes, the muse,
Went to pay me a visit whilst I took a snooze.

I'd visions of carrot cake, candy, and cheese
When dashed open my window and entered a breeze
That stirred me to consciousness, albeit in time
To see my face plastered in pie of key lime.

And once I'd removed the fruit from my eyes
And put on my spectacles did I realize
That before me presented a most divine spectre
Who clearly possessed the Key Lime Projector.

"It's a fulcrum, you see!" he began to explain,
"All I use is this crank to cause the right strain,
"Then releasing its fetter it launches sky high
"The juiciest pie of key lime in your eye!"

I sat there immobile for what seemed a year,
As the spectre protested I his genius revere,
When clearly it came, the fine revelation,
Of his piety, honor, achievements, and station.
With his little old catapult, so lively and quick,
I knew in a moment that this must be …