gpt4 book ai didi

java - 从 map 中检索第一个和第二个值

转载 作者:行者123 更新时间:2023-11-30 04:29:17 25 4
gpt4 key购买 nike

从 map 中获取第一个值和第二个值的最佳方法是什么。我正在尝试读取 tableLists map 并从 map 中获取第一个和第二个值

下面是我的代码,其中 ReadTableConnectionInfo 是类。

private final LinkedHashMap<String, ReadTableConnectionInfo> tableLists;

ReadTableConnectionInfo table = tablePicker();


private ReadTableConnectionInfo tablePicker() {

Random r = new SecureRandom();
ReadTableConnectionInfo table;

if (r.nextFloat() < Read.percentageTable / 100) {
table = get first value from tableLists map
} else {
table = get second value from tableLists map
}

return table;
}

最佳答案

假设您确定 LinkedHashMap 至少包含两个值,您可以这样做:

Iterator<Map.Entry<String, ReadTableConnectionInfo >> it = tableLists.entrySet().iterator();
if (r.nextFloat() < Read.percentageTable / 100) {
table = it.next().getValue();
} else { //since you have an else, you have to re-ignore the first value just below
it.next(); // ignoring the first value
table = it.next().getValue(); //repeated here in order to get the second value
}

关于java - 从 map 中检索第一个和第二个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15048391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com