如何从现有地图获取给定双精度值的最接近值的键< String double> [英] how to get key of the most nearest value of a given double value from an existing map <String double>

查看:144
本文介绍了如何从现有地图获取给定双精度值的最接近值的键< String double>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我需要使用 BiMap 获取double值的关键值。

  BiMap< String,Double> mapObj = HashBiMap.create(); 
mapObj.put(a1,3.58654);
mapObj.put(a2,4.1567);
mapObj.put(a3,4.2546);

对于像4.0156这样的特殊值,我必须得到键值a2 ..也就是说, p>

  Double value = 4.0156; 
mapObj.inverse()。get(value)= a2

我尝试了很多方法,它始终为空,因为没有完全匹配。请任何人都帮助我...如果我选择了错误的方式,请更正它,因为我是Java新手。

首先:您可能想要:

 地图< String,Double> mapObj = HashMap<>(); 
mapObj.put(a1,3.58654);
mapObj.put(a2,4.1567);
mapObj.put(a3,4.2546);
mapObj.put(a4,4.1567); //重复值

然后您需要一个具有最接近值的反向查找。



为此,最好让所有条目按值排序。

 列表< Map.Entry< String,Double>>这不能是一个Set,因为这个值会出现多次。 entries = new ArrayList<>(mapObj.entrySet()); 
比较器< Map.Entry< String,Double>> cmp =(1hs,rhs) - >
Double.compare(lhs.getValue(),rhs.getValue());
Collections.sort(entries,cmp);

我知道Java中没有包含这种结构的数据结构。虽然可能有。
为了不丢失信息我使用Map.Entry,键值对。这需要一个比较值的值。
为简单起见,我在这里借用了Java 8语法。



现在搜索:

  Map.Entry< String,Double>最近(double值){
int index = Collections.binarySearch(entries,cmp);
if(index< 0){//未找到
index = -index + 1; //插入位置
Map.Entry< String,Double> before = index!= 0? entries.get(i - 1):null;
Map.Entry< String,Double> after = index< entries.size()?
entries.get(i):null;
if(before == null&&== after null){
return null;
} else if(before == null){
return after;
} else if(after == null){
return before;
}
返回值--VESTVALUE()< after.getValue() - 值?之前:之后;
}
返回entries.get(index);

$ / code>

要在增量内查找值的子列表,需要使用索引。



现在每个搜索的费用都是N,这是可以接受的。


Since I need to get the 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 a particular value like 4.0156 I must get the key value a2.. that is if,

Double value=4.0156; 
mapObj.inverse().get(value)=a2 

I tried many ways but its getting always null, since there is no exact match. please anyone help me... if I have chosen a wrong way please correct it because I'm new in Java.

解决方案

First: you probably want to have:

Map<String, Double> mapObj = HashMap<>();
mapObj.put("a1", 3.58654);
mapObj.put("a2", 4.1567);
mapObj.put("a3", 4.2546);
mapObj.put("a4", 4.1567); // Repeated value

And then you want a reverse lookup with nearest value.

For that it would be nice to have all entries sorted by value. This cannot be a Set because of a value occuring multiple times.

List<Map.Entry<String, Double>> entries = new ArrayList<>(mapObj.entrySet());
Comparator<Map.Entry<String, Double>> cmp = (lhs, rhs) ->
    Double.compare(lhs.getValue(), rhs.getValue());
Collections.sort(entries, cmp);

I know of no data structure in Java that combines this. Though there probably is. To not lose information I use the Map.Entry, key-value pair. That needs a Comparator on the values. For shortness, I have borrowed from Java 8 syntax here.

Now search:

Map.Entry<String, Double> nearest(double value) {
    int index = Collections.binarySearch(entries, cmp);
    if (index < 0) { // Not found
        index = -index + 1; // The insertion position
        Map.Entry<String, Double> before = index != 0 ? entries.get(i - 1) : null;
        Map.Entry<String, Double> after = index < entries.size() ?
                entries.get(i) : null;
        if (before == null && after == null) {
            return null;
        } else if (before == null) {
            return after;
        } else if (after == null) {
            return before;
        }
        return value - before.getValue() < after.getValue() - value ? before : after;
    }
    return entries.get(index);
}

To find a sub list of values inside a delta, one would need to use the index.

Now every search costs ²log N, which is acceptable.

这篇关于如何从现有地图获取给定双精度值的最接近值的键&lt; String double&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆