简体   繁体   English

Java - 如何从最接近特定数字的hashmap中查找值?

[英]Java - How to find a value from a hashmap that is the closest to a particular number?

Hi I have a HashMap<String, Double> and also a function which returns a double value known as answer . 嗨,我有一个HashMap<String, Double> ,还有一个返回一个称为answer的double值的函数。 I want to check which value in the HashMap is the closest to the answer and then grab that value's key and print it. 我想检查HashMap中哪个值最接近答案,然后获取该值的键并打印出来。

HashMap<String, Double> output = new HashMap<String, Double>();


contents
("A", 0)
("B", 0.25)
("C", 0.5)
("D", 0.75)
("E", 1)

Suppose the answer to one of my functions was 0.42, how can I check which value it is closest to and then grab the key to that value. 假设我的一个函数的答案是0.42,我如何检查它最接近的值,然后获取该值的键。 I cant switch around the key and value of the HashMap (as a previous function assigns the values equally to each letter), otherwise it would be better to go through each key and get the value. 我无法切换HashMap的键和值(因为前一个函数将值分配给每个字母),否则最好通过每个键并获取值。

If your values are unique, you can use a TreeMap , which implements NavigableMap , which has the nice ceilingKey and floorKey methods: 如果您的值是唯一的,则可以使用实现NavigableMapTreeMap ,它具有nice ceilingKeyfloorKey方法:

    NavigableMap<Double, String> map = new TreeMap<>();
    map.put(0d, "A");
    map.put(0.25, "B");
    map.put(0.5, "C");
    map.put(0.75, "D");
    map.put(1d, "E");

    double value = 0.42;
    double above = map.ceilingKey(value);
    double below = map.floorKey(value);

    System.out.println(value - below > above - value ? above : below); //prints 0.5

Note: both methods can return null if value is less (resp. greater) than the smallest / largest key. 注意:如果value小于(最大),则两个方法都可以返回null,而不是最小/最大键。

With a HashMap , you have to go through each entry. 使用HashMap ,您必须浏览每个条目。

However, if performance is important and you're going to be finding several entries this way, you could create another collection: a list or an array of the entries in the hash map, sorted by value. 但是,如果性能很重要并且您将以这种方式查找多个条目,则可以创建另一个集合:哈希映射中的条目列表或数组,按值排序。 You could then do a binary search to find the entry with the nearest value more efficiently, and return the key. 然后,您可以进行二进制搜索,以更有效地查找具有最近值的条目,并返回密钥。 Of course that doesn't help if you're only going to do this once per map... 当然,如果你每个地图只打算这样做一次,这无济于事......

The concept of 'closest' is not really meaningful for Hashed data structures. “最接近”的概念对于Hashed数据结构并没有多大意义。 The primary goal of efficient hashing algorithms is collision avoidance, which is directly contrary to closeness. 有效散列算法的主要目标是避免碰撞,这与紧密度直接相反。 Either you have a collision, or not. 要么你有碰撞,要么没有碰撞。

If you were asking this for an ordered key data structure (eg. TreeMap ), the answer would be different. 如果您要求这是一个有序的密钥数据结构(例如TreeMap ),答案会有所不同。

HashMap isn't the best structure to do this. HashMap不是最好的结构。 You can get output.keySet() and check each value, for example: 您可以获取output.keySet()并检查每个值,例如:

for(String key:output.keySet()){
   Double temp=Math.abs(output.get(key)-answer);
   if(temp<min){
     min=temp;
     nearest=key;
   }
}

but it's not the best way. 但这不是最好的方式。 you are forced to use a hashmap? 你被迫使用hashmap? By the way, this isn't a problem if you have always five answer to check... 顺便说一下,如果你总是有五个答案来检查,这不是问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM