2,"ab"->3,"c"->4) scala> m.get("a"); scala> println(res.get) 2 sca...">

Using a regular expression to access values ​​from a map in keys

val m = Map("a"->2,"ab"->3,"c"->4) scala> m.get("a"); scala> println(res.get) 2 scala> m.get(/a\.*/) // or something similar. 

Can I get a list of all key-value pairs, where the key contains "a" without having to repeat it all over the map, doing something as simple as setting a regular expression in the key value?

Thanks in advance!

+5
source share
1 answer

No, you cannot do this without repeating the entire map. In fact, I can’t even think of a single data structure that would allow it, not to mention the API.

Of course, the iteration is pretty simple:

 m.filterKeys(_ matches "a.*") 
+9
source

Source: https://habr.com/ru/post/1205750/


All Articles