If I have
val key1 = "mykey"
val key2 = 427
Is it possible to hash by both? I could do something like
val compoundKey = key1 + "#" + key2
myhash.put(compoundKey, value)
However that seems a bit clunky
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I always prefer the new data type over Tuple for three reasons:
You have a name, especially in compiler warnings and an “
expected CompoundKey” is clearer than a “expected Tuple2[String,String]“. Or it just helps you with a type annotation to make your own code more readable, especially in nested structures like Mapsval k: CompoundKey = expensiveComputationOrNonObviousMethodCallsInARow(...)val keyMap: Map[CompoundKey,Key]instead ofMap[(String,String),Key]Access to the subkeys in CompoundKey can be done by name:
val ckey = CompoundKey("foo","bar")ckey.key1instead ofckey._1String, later on. That means if you changeStringto whatever that you doesn´t have to changeTuple2[String,String]all over your code. OnlyCompoundKeyhas to be adapted.(I even would use a wrapper
case class Key(str: String)for the key class)