如何在 Hashmaps Kotlin 中允许重复键?

哈加尔穆罕默德

我需要一个允许重复键的哈希图的 kotlin 代码

class HashMap<K, V> : MutableMap<K, V>
天福04

假设您的键和值都是字符串,您可以创建一个MutableMap<String, MutableList<String>>.

val eventsBySubject = mutableMapOf<String, MutableList<String>>()

// Adding a value "physics" for key "test":
eventsBySubject.getOrPut("test", ::mutableListOf).add("physics")

// Getting all the values for a key "quiz":
val quizzes: List<String> = eventsBySubject["quiz"].orEmpty()

// Removing a single value "lab" for key "test":
eventsBySubject["test"]?.remove("lab")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章