[英]How to enumerate hashtable's key from specific value
For example I have a Hashtable like, and I have a value in Double d=99.22 variable, Here I want to fetch or find the key related to this value. 例如,我有一个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;
Thanks in advance 提前致谢
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);
I hope it'll be helpful. 希望对您有所帮助。
Try this code after the code snippet you have mentioned in the post: 在帖子中提到的代码段之后尝试以下代码:
for(Map.Entry<String, Double> e : balance.entrySet()){ if(e.getValue()== d.doubleValue()){ System.out.println(e.getKey()); } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.