Can you keep a keyMap:
```scala
val keyMap = ks.map { k => (k.toString, k) }.toMap
```
And then use it below instead of the cast?  This is a little defensive (your code would work in almost all cases), but I don't like the cast.