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<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()); } }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.