地图.get(),. encinsinsKey()方法不起作用

时间:2015-12-07 05:52:14

标签: java dictionary

我发现有很多重复的问题,但我无法在我的代码中解决它。

这是我的问题,我有一张地图,

TreeMap<String,Double> relevantType_Confidence_Map_Aggregated = AggregateTwoTreeMaps.Aggregate2nd3rdYearResult(relevantType_Confidence_Map_Sorted, relevantType_Confidence_Third);
// I think the process of making relevantType_Confidence_Map_Aggregated isn't important in this question.
System.out.println(relevantType_Confidence_Map_Aggregated);
{http://www.w3.org/2002/07/owl#Thing=0.9698529984028573, http://dbpedia.org/ontology/Agent=0.9679645061134368, http://dbpedia.org/ontology/Person=0.9485782773178664}

而且,我有String变量。

String className = "http://dbpedia.org/ontology/Agent";

但是,get()或containsKey()方法无法在我当前的代码中使用。

System.out.println(relevantType_Confidence_Map_Aggregated.get(className));
null
System.out.println(relevantType_Confidence_Map_Aggregated.containsKey(className));
false

对于短期解决方案,我迭代条目并获得我想要的值,但我想知道应该修改什么来使用内置方法(get,containsKey)

for(Entry<String, Double> e : relevantType_Confidence_Map_Aggregated.entrySet()){
        if(e.getKey().equals(className)){
            System.out.println(e.getValue());
        }
    }
0.9679645061134368  

提前感谢您的回复。

补充代码

方法Aggregate2nd3rdYearResult

public static TreeMap<String, Double> Aggregate2nd3rdYearResult(
        TreeMap<String, Double> relevantType_Confidence_Map_Sorted,
        TreeMap<String, Double> relevantType_Confidence_Third) {
    // TODO Auto-generated method stub

    TreeMap<String,Double> relevantType_Confidence_Map_Aggregated = new TreeMap<String,Double>();       

    relevantType_Confidence_Map_Aggregated = average(relevantType_Confidence_Map_Sorted, relevantType_Confidence_Third);

    ValueComparator bvc =  new ValueComparator(relevantType_Confidence_Map_Aggregated);
    TreeMap<String,Double> relevantType_Confidence_Map_Aggregated_Sorted = new TreeMap<String,Double>(bvc);
    relevantType_Confidence_Map_Aggregated_Sorted.putAll(relevantType_Confidence_Map_Aggregated);

    return relevantType_Confidence_Map_Aggregated_Sorted;
}

方法平均值

public static TreeMap<String, Double> average(TreeMap<String, Double> first,
        TreeMap<String, Double> second) {
    // optimization (copy the largest tree map and iterate over the
    // smallest)

    TreeMap<String, Double> result = new TreeMap<String, Double>();
    double threshold = 0.001;

    for (java.util.Map.Entry<String, Double> e : first.entrySet()) {
        Double l = result.get(e.getKey());
        result.put(e.getKey(), e.getValue() + (l == null ? 0 : l));
    }
    for (java.util.Map.Entry<String, Double> e : second.entrySet()) {
        Double l = result.get(e.getKey());
        result.put(e.getKey(), e.getValue() + (l == null ? 0 : l));
    }
    for (java.util.Map.Entry<String, Double> e : result.entrySet()) {
        Double l = result.get(e.getKey());
        result.put(e.getKey(), l/2);
    }


    Iterator it = result.entrySet().iterator();
    while (it.hasNext()){
    java.util.Map.Entry<String, Double> item = (java.util.Map.Entry<String, Double>) it.next();
       if(item.getValue() < threshold){
           it.remove();
       }

    }

    return result;
}

Class ValueComparator

import java.util.Comparator;
import java.util.Map;

public class ValueComparator implements Comparator<String> {

    Map<String, Double> base;
    public ValueComparator(Map<String, Double> base) {
        this.base = base;
    }

    // Note: this comparator imposes orderings that are inconsistent with equals.    
    public int compare(String a, String b) {
        if (base.get(a) >= base.get(b)) {
            return -1;
        } else {
            return 1;
        } // returning 0 would merge keys
    }

}

1 个答案:

答案 0 :(得分:1)

ValueComparator使用的TreeMap不正确。它不仅与equals不一致,而且还违反了sgn(compare(x, y)) == -sgn(compare(y, x))对所有x和y的要求。具有此类比较器的TreeMap可能表现不正确。

可能的解决方案是在a.compareTo(b)时使用base.get(a) == base.get(b)(因此即使具有相同值的键具有某些已定义的顺序):

public int compare(String a, String b) {
    double diff = base.get(a) - base.get(b);
    if (diff > 0) {
        return -1;
    } else if (diff < 0) {
        return 1;
    } else {
        return a.compareTo(b);
    }
}