What is the proper way to remove elements from a scala mutable map using a predicate

后端 未结 3 998
梦毁少年i
梦毁少年i 2021-02-13 06:29

How to do that without creating any new collections? Is there something better than this?

val m = scala.collection.mutable.Map[String, Long](\"1\" -> 1, \"2\"         


        
3条回答
  •  孤街浪徒
    2021-02-13 07:26

    If you are using an immutable.Map, in 2.7 it might have to be something like:

    def pred(k: Int, v: String) = k % 2 == 0
    
    m --= (m.filter(pred(_, _)).keys
    

    As there is no retain method available. Obviously in this case m must be declared as a var

提交回复
热议问题