2016-05-30 48 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(); 
    } 
} 
相关问题