[英]How can I retrieve a value from a list of key/value pairs with the key from a given pair?
我有以下幾對
val mItemArray : ArrayList<Pair<Long, String>> = arrayListOf()
I/System.out: [Pair{256 yeet}, Pair{128 yeah_boy}, Pair{64 spaghet}, Pair{32 screaming_kid}, Pair{16 nice}, Pair{8 leeroy}, Pair{4 wow}, Pair{2 damn_son}, Pair{1 baby_a_triple}]
我想從給定鍵(Long)的一對中檢索String值,即1或32。我該怎么做?
我正在這樣填充列表:
var index = 0
var uniqueID = 1L
for (item in rawItems) {
mItemArray.add(index, Pair(uniqueID, item.name))
println(item.name)
index += index
uniqueID += uniqueID
}
您可以使用find()
:
mItemArray.find { it.first == id }?.second
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.