例如我有一个Hashtable,我在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;
提前致谢
答案 0 :(得分:1)
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);
我希望它会有所帮助。
答案 1 :(得分:0)
在帖子中提到的代码段后尝试此代码:
for(Map.Entry<String, Double> e : balance.entrySet()){
if(e.getValue()== d.doubleValue()){
System.out.println(e.getKey());
}
}