Java 如何从现有映射中获取给定双值的最接近值的键<;双字符串>;
因为我需要获得双值的键值,所以我正在使用Java 如何从现有映射中获取给定双值的最接近值的键<;双字符串>;,java,hashmap,treemap,bimap,Java,Hashmap,Treemap,Bimap,因为我需要获得双值的键值,所以我正在使用BiMap BiMap<String,Double>mapObj = HashBiMap.create(); mapObj.put("a1",3.58654); mapObj.put("a2",4.1567); mapObj.put("a3",4.2546); 我尝试了很多方法,但它总是空的,因为没有精确的匹配。请任何人帮帮我。。。如果我选择了一个错误的方法,请更正它,因为我是Java新手。像这样迭代一个条目集就可以了 String near
BiMap
BiMap<String,Double>mapObj = HashBiMap.create();
mapObj.put("a1",3.58654);
mapObj.put("a2",4.1567);
mapObj.put("a3",4.2546);
我尝试了很多方法,但它总是空的,因为没有精确的匹配。请任何人帮帮我。。。如果我选择了一个错误的方法,请更正它,因为我是Java新手。像这样迭代一个条目集就可以了
String nearestKey = null;
Double nearestValue = null;
for(Entry<String,Double> e : map.entrySet()){
//if e.getValue() is nearer than prev nearestValue
// nearestKey = e.getKey();
// nearestValue = e.getValue();
}
String nearestKey=null;
Double nearestValue=null;
对于(条目e:map.entrySet()){
//如果e.getValue()比prev nearestValue更接近
//nearestKey=e.getKey();
//nearestValue=e.getValue();
}
您只需编写一个函数来确定该键是否比前一个键更接近,并更新变量。您必须迭代所有键-值对,然后选择一个值与您要查找的值最接近的键
public String searchClosest(Map<String,Double> map, double value)
{
double minDistance = Double.MAX_VALUE;
String bestString = null;
for (Map.Entry<String,Double> entry : map.entrySet()) {
double distance = Math.abs(entry.getValue() - value);
if (distance < minDistance) {
minDistance = distance;
bestString = entry.getKey();
}
}
return bestString;
}
公共字符串搜索最近(映射映射,双值)
{
double minDistance=double.MAX_值;
字符串bestString=null;
对于(Map.Entry:Map.entrySet()){
双距离=Math.abs(entry.getValue()-value);
if(距离<距离){
距离=距离;
bestString=entry.getKey();
}
}
返回最佳字符串;
}
将键和值倒置并应用以下方法将其转换为已排序的地图
private static Double getClosest(TreeMap<Double, String> mySet, Double d) {
if (mySet.ceilingKey(d) != null && mySet.floorKey(d) != null){
if( Math.abs(d - mySet.ceilingKey(d)) < Math.abs(d - mySet.floorKey(d)) )
return mySet.ceilingKey(d);
else
return mySet.floorKey(d);
}
else if (mySet.ceilingKey(d) == null && mySet.floorKey(d) != null) {
return mySet.floorKey(d);
} else if (mySet.ceilingKey(d) != null && mySet.floorKey(d) == null) {
return mySet.ceilingKey(d);
} else
return null;
}
private static Double getClosest(TreeMap mySet,Double d){
if(mySet.ceilingKey(d)!=null&&mySet.floorKey(d)!=null){
if(Math.abs(d-mySet.ceilingKey(d))
首先:您可能想要:
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
Map-mapObj=HashMap();
mapObj.put(“a1”,3.58654);
mapObj.put(“a2”,4.1567);
mapObj.put(“a3”,4.2546);
mapObj.put(“a4”,4.1567);//重复值
然后,您需要使用最接近的值进行反向查找
因此,将所有条目按值排序会很好。由于某个值出现多次,因此无法设置该值
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);
List entries=newarraylist(mapObj.entrySet());
比较器cmp=(左侧、右侧)->
Double.compare(lhs.getValue(),rhs.getValue());
集合。排序(条目,cmp);
据我所知,Java中没有任何数据结构结合了这一点。虽然可能有。
为了不丢失信息,我使用Map.Entry和键值对。这需要一个值的比较器。
简而言之,我在这里借用了Java8语法
现在搜索:
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);
}
Map.Entry最近(双倍值){
int index=Collections.binarySearch(条目,cmp);
如果(索引<0){//未找到
index=-index+1;//插入位置
Map.Entry before=index!=0?entries.get(i-1):null;
Map.Entry after=索引
要在增量中查找值的子列表,需要使用索引
现在,每个搜索都需要2 log N,这是可以接受的。如果您有一个包含所有
双值的列表,您可以使用二进制搜索。您认为如何命名变量bestKey
,而不是bestString
?)谢谢你,先生。。。非常感谢您的回复。。。没有重复的值,因此排序可能会起作用。。。谢谢你建议创建一个搜索方法。。。
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);
}