久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Java - 如何從最接近特定數(shù)字的哈希圖中找到一個(gè)

Java - How to find a value from a hashmap that is the closest to a particular number?(Java - 如何從最接近特定數(shù)字的哈希圖中找到一個(gè)值?)
本文介紹了Java - 如何從最接近特定數(shù)字的哈希圖中找到一個(gè)值?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

您好,我有一個(gè) HashMap<String, Double> 以及一個(gè)返回雙精度值的函數(shù),稱為 answer.我想檢查 HashMap 中的哪個(gè)值最接近答案,然后獲取該值的鍵并打印它.

Hi I have a HashMap<String, Double> and also a function which returns a double value known as answer. 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<String, Double> output = new HashMap<String, Double>();


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

假設(shè)我的一個(gè)函數(shù)的答案是 0.42,我如何檢查它最接近哪個(gè)值,然后獲取該值的鍵.我無法切換 HashMap 的鍵和值(因?yàn)橹暗暮瘮?shù)將值平均分配給每個(gè)字母),否則最好遍歷每個(gè)鍵并獲取值.

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. 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.

推薦答案

如果你的值是唯一的,你可以使用 TreeMap,實(shí)現(xiàn) NavigableMap,它有很好的 ceilingKeyfloorKey 方法:

If your values are unique, you can use a TreeMap, which implements NavigableMap, which has the nice ceilingKey and floorKey methods:

    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

注意:如果 value 小于(或大于)最小/最大鍵,則兩種方法都可以返回 null.

Note: both methods can return null if value is less (resp. greater) than the smallest / largest key.

這篇關(guān)于Java - 如何從最接近特定數(shù)字的哈希圖中找到一個(gè)值?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Convert List of Strings into Map using Java-8 Streams API(使用 Java-8 Streams API 將字符串列表轉(zhuǎn)換為 Map)
Getting data from JSON(從 JSON 獲取數(shù)據(jù))
java linkedhashmap iteration(javalinkedhashmap迭代)
Converting a list of objects to Map(將對(duì)象列表轉(zhuǎn)換為 Map)
Create a HashMap with a fixed Key corresponding to a HashSet. point of departure(用一個(gè)固定的Key對(duì)應(yīng)一個(gè)HashSet創(chuàng)建一個(gè)HashMap.出發(fā)點(diǎn))
HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found(HttpMessageConverter 異常:RestClientException:無法寫入請(qǐng)求:找不到合適的 HttpMessageConverter) - IT屋-程序員
主站蜘蛛池模板: 欧美一区不卡 | 精品中文字幕一区二区三区 | 久久久久无码国产精品一区 | 国产精品欧美精品 | 国产精品国产三级国产aⅴ入口 | 波波电影院一区二区三区 | 久久伊人免费视频 | www.97国产| 亚洲国产精品成人无久久精品 | 国产精品99久久久精品免费观看 | 国产成人福利 | 国产精品1 | 青青草国产在线观看 | 狠狠狠色丁香婷婷综合久久五月 | 欧美激情综合色综合啪啪五月 | 91av在线视频观看 | 日韩精品人成在线播放 | 欧美精品在欧美一区二区少妇 | 久久国产三级 | av一区二区三区四区 | 久久久久久久香蕉 | 午夜av电影 | 日韩欧美国产一区二区三区 | 国产精品日韩一区二区 | 一区二区三区精品在线 | 天天色影视综合 | 九九色九九 | 久在线精品视频 | 亚洲综合小视频 | 国产精品久久国产精品 | 久久久久久综合 | 日本一二三区电影 | 欧美电影免费观看高清 | 日韩精品视频在线免费观看 | 先锋av资源网 | 久久精品亚洲国产奇米99 | 久久综合亚洲 | 欧美在线a | 午夜欧美日韩 | 综合色播 | 97人人草|