moon
moon

Reputation: 1832

What is the retrieval performance of tuple as values in a hashmap in Scala?

I have a hash map, using string as key, values are list of strings of size 2. I am trying to get the fastest performance time when getting the values back. The values will always contain only 2 strings. The question are:

  1. what is the best container in this use case? List or Tuple?
  2. What is the retrieval performance of tuple in Scala? is it equal to/faster than list?

By the way, I couldn't find anything on Scala tuple's performance. Please share if you know of any.

Upvotes: 0

Views: 1549

Answers (1)

colinjwebb
colinjwebb

Reputation: 4378

If you have a Tuple of two things, you will be using the case class Tuple2. As you can see, it is just a class that takes two parameters to instantiate.

If you're concerned about the performance of Lists versus Tuples, I suggest you write a benchmark. I find using ScalaMeter exceedingly useful when doing that. I suspect the performance of both will be the same.

If you're concerned about readability and reasonability of your code, I would suggest using a Tuple. If the values will always contain two values, then using Tuple2 is the better choice. Using a List implies that it could potentially contain less, or more, than two.

Upvotes: 6

Related Questions