2016-05-30 80 views
0

如何從散列圖中減去兩個值後返回最小差異的鍵?從散列表中減去兩個值後返回最小差異的關鍵?

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(); 
    } 
} 

請幫助。謝謝。

+0

你的問題可能是因爲你不喜歡你應該比較字符串。使用'key.equals(「One」);'而不是'=='。讓我知道如果這有助於我可以關閉你的問題。 – Maroun

+0

另外請注意,您沒有將「差異」與先前的結果進行比較,因此最終結果將是最後一次迭代中的最後一次差異。 – Maroun

+0

@MarounMaroun我知道。這就是爲什麼我要問如何從迭代中獲得最小的差異,並且我改變了!= to!key.equals(「One」) –

回答

0

可以實現,使用類似以下內容:

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; 
    } 

} 
0

嘗試使用代碼類似的東西:

String smallestKey; 

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

我覺得你的問題是比你想象的簡單。我知道這是不是最好的,但這裏有一個解決方案:

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(); 
    } 
} 
相關問題