如何使用给定对中的键从键/值对列表中检索值?

问题描述 投票:0回答:1

我有以下列表对

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}]

我想从一对中检索String值,给定密钥(Long),即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
}
java kotlin keyvaluepair
1个回答
1
投票

你可以使用find()

mItemArray.find { it.first == id }?.second
© www.soinside.com 2019 - 2024. All rights reserved.