Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map with only certain keys

Tags:

scala

With regard to maps in Scala, if ms - (k, 1, m) returns the map containing all mappings of ms except for any mapping with the given keys, x, 1 and m.

Then, what statement will return a map of all mappings of ms with only the given keys, x, 1 and m. i.e. I'm looking for the subset of ms where only k, 1 and m are keys.

This works, but it is terrible:

scala> val originalMap = Map("age" -> "20", "name" -> "jack", "hobby" -> "jumping")
ms: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(age -> 20, name -> jack, hobby -> jumping)

scala> val interestingKeys = List("name", "hobby")
interesting: List[java.lang.String] = List(name, hobby)

scala> val notInterestingMap = originalMap -- interestingKeys
notInterestingMap: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(age -> 20)

scala> val interestingMap = originalMap -- notInterestingMap.keySet
interestingMap: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(name -> jack, hobby -> jumping)
like image 892
Jack Avatar asked Feb 15 '12 12:02

Jack


2 Answers

filterKeyscan help:

scala> originalMap.filterKeys(interestingKeys.contains)
res0: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(name -> jack, hobby -> jumping)
like image 101
Nicolas Avatar answered Nov 21 '22 06:11

Nicolas


Because filterKeys filters based on an arbitrary predicate, it has to consider every key in the map. This might be fine or not, depending on how large the map is, etc., but it's definitely not necessary for the operation you describe. I'd use something like the following:

interestingKeys.flatMap(k => originalMap.get(k).map((k, _))).toMap

This will be O(n) or O(n log m) depending on your map implementation (where n is the size of interestingKeys and m is the size of the map), instead of O(m log n) or O(mn).

If you really want your ~ operator, you can use the pimp-my-library pattern:

class RichMap[A, B](m: Map[A, B]) {
  def ~(ks: A*) = ks.flatMap(k => m.get(k).map((k, _))).toMap
}

implicit def enrichMap[A, B](m: Map[A, B]) = new RichMap(m)

Now originalMap ~ ("name", "hobby") returns Map(name -> jack, hobby -> jumping), as you'd expect.

like image 24
Travis Brown Avatar answered Nov 21 '22 08:11

Travis Brown