gpt4 book ai didi

java - 如何从特定值枚举哈希表的键

转载 作者:行者123 更新时间:2023-12-01 11:33:21 25 4
gpt4 key购买 nike

例如,我有一个类似的哈希表,并且我在 Double d=99.22 变量中有一个值,这里我想获取或查找与该值相关的键。

Hashtable<String, Double> balance = new Hashtable<String, Double>();

balance.put("Zara", new Double(3434.34));
balance.put("Mahnaz", new Double(123.22));
balance.put("Ayan", new Double(1378.00));
balance.put("Daisy", new Double(99.22));
balance.put("Qadir", new Double(-19.08));

System.out.println(balance);

Double d = 99.22;

提前致谢

最佳答案

 Hashtable<String, Double> balance = new Hashtable<String, Double>();

balance.put("Zara", new Double(3434.34));
balance.put("Mahnaz", new Double(123.22));
balance.put("Ayan", new Double(1378.00));
balance.put("Daisy", new Double(99.22));
balance.put("Qadir", new Double(-19.08));

System.out.println(balance);
Double d = 99.22;



String key=null;
for(Map.Entry entry: balance.entrySet()){
if(d.equals(entry.getValue())){
key = (String) entry.getKey();
break;
}
}
System.out.println("got key from value in hashtable key: "+ key +" value: " + d);

希望对您有所帮助。

关于java - 如何从特定值枚举哈希表的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30238577/

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