java - how to get key of the most nearest value of a given double value from an existing map <String double> -


since need key values of double value i' m using bimap.

bimap<string,double>mapobj = hashbimap.create(); mapobj.put("a1",3.58654); mapobj.put("a2",4.1567); mapobj.put("a3",4.2546); 

for particular value 4.0156 must key value a2.. if,

double value=4.0156;  mapobj.inverse().get(value)=a2  

i tried many ways getting null, since there no exact match. please me... if have chosen wrong way please correct because i'm new in java.

you have iterate through key-value pairs , select 1 value nearest value you're looking for.

public string searchclosest(map<string,double> map, double value) {     double mindistance = double.max_value;     string beststring = null;      (map.entry<string,double> entry : map.entryset()) {         double distance = math.abs(entry.getvalue() - value);         if (distance < mindistance) {             mindistance = distance;             beststring = entry.getkey();         }     }      return beststring; } 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -