【发布时间】:2018-08-13 01:56:58
【问题描述】:
我可能遗漏了一些非常明显的东西:如何在 Kotlin 中有效地过滤和迭代 HashMap 的条目?
我想做以下事情:
myMap.filterValues{ someCondition }.forEach { doSomethingWithTheEntry }
如何避免创建中间对象? filterValues 会创建一个 HashMap,这里不需要。
我当然可以写
myMap.forEach { if(someCondition) doSomethingWithTheEntry }
但函数式过滤器方法看起来更优雅。
【问题讨论】: