我需要一个允许重复键的哈希图的 kotlin 代码
class HashMap<K, V> : MutableMap<K, V>
假设您的键和值都是字符串,您可以创建一个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] 删除。
我来说两句