SUNDONG
SUNDONG

Reputation: 2769

Map .get(), .containsKey() method doesn't work

I found out there are many duplicate questions, but I couldn't solve it in my code.

Here is my problem, I have a map,

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}

And, I have String variable.

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

However, get() or containsKey() methods don't work in my current code.

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

For short-term solution, I iterated over entries and get the value that I want, but I want to know what should I modify to use built-in method(get, containsKey)

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

Thanks in advance for kind replies.


Supplementary code

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

Method average

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
    }

}

Upvotes: 1

Views: 256

Answers (1)

x22
x22

Reputation: 541

The ValueComparator used by the TreeMap is incorrect. It is not only inconsistent with equals but it also violates the requirement that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y. TreeMap with such Comparator can behave incorrectly.

Possible solution is to use a.compareTo(b) when base.get(a) == base.get(b) (so even keys with same value have some defined order):

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

Upvotes: 1

Related Questions