簡體   English   中英

從hashmap減去兩個值后返回最小差異的鍵?

[英]Return the key of the smallest difference after substracting two values from hashmap?

從哈希圖中減去兩個值后,如何返回最小差異的鍵?

first loop -> 10 - 8 = 2;
second loop -> 10 - 9 = 1;
third loop -> 10 - 7 = 3;

therefore second loop -> 10 - 9 = 1 is the smallest, so the key is "Three".

import java.util.HashMap;

public class Difference {
    HashMap<String,Double> hashMap = new HashMap<>();
    double firstValue = 0;
    double secondValue = 0;
    double difference = 0;
    public Difference() {   
        hashMap.put("One", 10.0);
        hashMap.put("Two", 8.0);
        hashMap.put("Three", 9.0);
        hashMap.put("Four", 7.0);

        firstValue = hashMap.get("One");
        for (String key : hashMap.keySet()) {
            if(!key.equals("One")) {
                secondValue = hashMap.get(key);
                difference = Math.abs(secondValue - firstValue);
            }
        }
    }

    public static void main(String[] args) {
        new Difference();
    }
}

請幫忙。 謝謝。

嘗試使用類似這樣的代碼:

String smallestKey;

if(difference !=0 && difference < Math.abs(secondValue - firstValue);){
   difference = Math.abs(secondValue - firstValue);
   smallestKey = key;
}

我認為您的問題比您想象的要簡單。 我知道這不是最好的方法,但這是一個解決方案:

import java.util.HashMap;

public class Difference {
    HashMap<String,Double> hashMap = new HashMap<>();
    double firstValue = 0;
    double secondValue = 0;
    double difference = 0;
    HashMap<Double, String> theMap = new HashMap<Double, String>();

    public Difference() {   
        hashMap.put("One", 10.0);
        hashMap.put("Two", 8.0);
        hashMap.put("Three", 9.0);
        hashMap.put("Four", 7.0);

        firstValue = hashMap.get("One");
        for (String key : hashMap.keySet()) {
            if(!key.equals("One")) {
                secondValue = hashMap.get(key);
                difference = Math.abs(secondValue - firstValue);
                theMap.put(difference, key);
            }
        }

        Set<Double> dbl = theMap.keySet();
        Double smallestDifference = findSmallest(dbl);
        String smallestValue = hashMap.get(smallestDifference);
    }

    public Double findSmallest(Set<Double> setDbl){
        Double smallest = 99999999.0;
        for(Double d : setDbl){
            if(d < smallest)
                smallest = d;
        }
        return smallest;
    }

    public static void main(String[] args) {
        new Difference();
    }
}

您可以使用以下方法實現此目的:

public class MainTest {

  public static void main(String[] args) {

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

    hashMap.put("One", 10.0);
    hashMap.put("Two", 8.0);
    hashMap.put("Three", 9.0);
    hashMap.put("Four", 7.0);
    hashMap.put("Five", 10.1);

    System.out.println(getSmallestDiffKeyJava8(hashMap, "One"));

  }

  /* This works only with java 8 */
  private static String getSmallestDiffKeyJava8(Map<String, Double> map, String constantKey) {
    double constant = map.get(constantKey);

    return map.entrySet().stream()
        .filter(entry -> !constantKey.equals(entry.getKey())) // Remove the constant from the values we process
        .map(entry -> new SimpleEntry<>(entry.getKey(), Math.abs(entry.getValue() - constant))) // Map to a new entry with the key and the diff
        .min((o1, o2) -> (int)(o1.getValue() - o2.getValue())) // Find the min
        .map(Entry::getKey)
        .get();


  }

  /* This works with older versions as well */
  private static String getSmallestDiffKey(Map<String, Double> map, String constantKey) {
    double constant = map.get(constantKey);
    String key = null;
    Double diff = null;

    for (Entry<String, Double> entry : map.entrySet()) {
      if (!constantKey.equals(entry.getKey())) {
        double d = Math.abs(entry.getValue() - constant);
        if (diff == null || diff > d) {
          diff = d;
          key = entry.getKey();
        }
      }
    }

  return key;
  }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM