gpt4 book ai didi

java - 如何从 LinkedHashMap 获取 key

转载 作者:行者123 更新时间:2023-11-30 10:14:14 24 4
gpt4 key购买 nike

如何从LinkedHashMap获取key?

例如,我有 LinkedHashMap,其中包含键和用户名,我需要在扫描仪的帮助下输入名称,然后我想查看该元素具有什么键,即我输入的元素:

static LinkedHashMap<Integer, String> names = new LinkedHashMap<>();
static Scanner sc = new Scanner(System.in);
static int i = 1;
stasic String name;

public static void main(String[] args) {
int b = 0;
while (b == 0) {
System.out.println("Input your name: ");
name = sc.nextLine;
names.put(i, name);
i += 1;
outName();
}
}

public static void outName() {
System.out.println("Input name: ");
name = sc.nextLine();
if (names.containsValue(name)) {
//I do not know what should I do here?
}
}

最佳答案

for (Map.Entry<String, ArrayList<String>> entry : names.entrySet()) {
String key = entry.getKey();
ArrayList<String> value = entry.getValue();
}

使用Map.Entry进行迭代

关于java - 如何从 LinkedHashMap 获取 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50963967/

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