TreeMap Key, Value 对存在,但是 .get(Key) returns 值为 null

TreeMap Key, Value pair exists, but .get(Key) returns the value as null

我有这段代码(如下),当我打印树形图时,我可以清楚地看到键值对。每个键都有一个值(输出中没有空值)。当我得到第一个键时,它会给我键,但是当我尝试根据键获取值时,它会 return null.

TreeMap<String, Double> cosinesimilarityvalues = simvalfordoc.returnsortedcosinesimilarityvalues();
System.out.println(cosinesimilarityvalues);
String topkey = cosinesimilarityvalues.firstKey();      
System.out.println(topkey);
Double topvalue = cosinesimilarityvalues.get(topkey);
System.out.println(topvalue);
topones.put(topkey, topvalue);

这是输出的一部分:

{article04_C9,article08_C12=0.0, article04_C9,article18_C10=0.0, article04_C9,article07_C1=0.0, article04_C9,article03_C10=0.0, article04_C9,article01_C10=0.0, article04_C9,article07_C10=0.0, article04_C9,article17_C10=0.0, article04_C9,article10_C10=0.0, article04_C9,article05_C10=0.0, article04_C9,article11_C10=0.0, article04_C9,article02_C10=0.0, article04_C9,article13_C10=0.0, article04_C9,article02_C13=5.676594773265355E-4, article04_C9,article02_C11=6.228132014119322E-4, article04_C9,article06_C10=6.732460014209593E-4, article04_C9,article12_C10=0.0011438670619737105, article04_C9,article03_C3=0.0011907203907551985, article04_C9,article03_C11=0.0012323612320990097}

所以我应该得到 article04_C9,article08_C12 作为 firstKey()(我这样做) 但是当我去检索与该键关联的值时,它 returns null.

这是我用来填充树状图的代码

HashMap<String, Double> cosinesimilarityvalues = new HashMap<String, Double>();
TreeMap<String, Double> sortedcosinesimilarityvalues = new TreeMap<String, Double>();
public void comparecosinesimilarityvalues(List<tfidfvalues> matrix, tfidfvalues currentvector) {
    String articlename = currentvector.returnarticlename();
    String foldername = currentvector.returnfoldername();
    ArrayList<Double> tfidfval = currentvector.returntfidfvaluesforrow();
    articlefolder = articlename+ "_" + foldername;
    CosineSimilarity calculator = new CosineSimilarity();
    for(int i = 0; i < matrix.size(); i++) {
        String compvectorarticlename = matrix.get(i).returnarticlename();
        String compvectorfoldername = matrix.get(i).returnfoldername();
        ArrayList<Double> compvector = matrix.get(i).returntfidfvaluesforrow();

        Double cosinesimilarity = calculator.CosineSimilarityCalc(tfidfval, compvector);

        String comparingwhat = compvectorarticlename + "_" + compvectorfoldername;

        String comparingthese = articlefolder + "," + comparingwhat;
        cosinesimilarityvalues.put(comparingthese, cosinesimilarity);
    }

    Iterator<Map.Entry<String, Double>> iterator = cosinesimilarityvalues.entrySet().iterator();
    while(iterator.hasNext()) {
        Map.Entry<String, Double> entry = iterator.next();
        if((entry.getValue() > 0.989 && entry.getValue() < 1) || entry.getValue() > 1) {
            iterator.remove();
        }
    }

    sortedcosinesimilarityvalues = sortMapByValue(cosinesimilarityvalues);
}

public TreeMap<String, Double> returnsortedcosinesimilarityvalues() {
    return sortedcosinesimilarityvalues;
}

这是我用来按值排序的函数...如果有帮助的话

来自 : https://www.programcreek.com/2013/03/java-sort-map-by-value/

class ValueComparator implements Comparator<String>{

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

    public ValueComparator(HashMap<String, Double> map){
        this.map.putAll(map);
    }

    @Override
    public int compare(String s1, String s2) {
        if(map.get(s1) >= map.get(s2)){
            return 1;
        }else{
            return -1;
        }   
    }
}

public TreeMap<String, Double> sortMapByValue(HashMap<String, Double> map){
    Comparator<String> comparator = new ValueComparator(map);
    //TreeMap is a map sorted by its keys. 
    //The comparator is used to sort the TreeMap by keys. 
    TreeMap<String, Double> result = new TreeMap<String, Double>(comparator);
    result.putAll(map);
    return result;
}

我不确定我做错了什么。请帮忙!

谢谢!


更新

我能够通过

检索到最高键和最高值
Map.Entry<String, Double> entry1 = cosinesimilarityvalues.firstEntry();
String topkey = entry1.getKey();
Double topvalue = entry1.getValue();

但我不知道为什么这个有效而另一种方法无效。 虽然我的代码现在可以工作了,但我希望我能找出其中的区别!

抱歉无法 post 在问题中发表评论,所以必须 post 在这里作为答案。也许您可以先尝试将整个字符串复制到 get 输入,看看是否仍然得到 null

Double topvalue = cosinesimilarityvalues.get(“article04_C9,article08_C12”);

我发现问题出在自定义比较器的覆盖比较方法上。以下复制了您的问题

    import java.util.Comparator;
import java.util.TreeMap;
public class MyTreeMapComparator {
    public static void main(String a[]){
        //the treemap sorts by key
        TreeMap<String, String> hm = new TreeMap<String, String>(new MyComp());
         //add key-value pair to TreeMap
         hm.put("java", "language");
         hm.put("computer", "machine");
         hm.put("india","country");
         hm.put("mango","fruit");
         System.out.println(hm.get("java"));
     }
}
class MyComp implements Comparator<String>{
    @Override
    public int compare(String str1, String str2) {
        if (str1.compareTo(str2) >= 0) {return 1;}
        else {return -1;}
    }
}

然后如果你改成这个就可以正常工作了

    import java.util.Comparator;
import java.util.TreeMap;
public class MyTreeMapComparator {
    public static void main(String a[]){
        //the treemap sorts by key
        TreeMap<String, String> hm = new TreeMap<String, String>(new MyComp());
         //add key-value pair to TreeMap
         hm.put("java", "language");
         hm.put("computer", "machine");
         hm.put("india","country");
         hm.put("mango","fruit");
         System.out.println(hm.get("java"));
     }
}
class MyComp implements Comparator<String>{
    @Override
    public int compare(String str1, String str2) {
        if (str1.compareTo(str2) == 0) {return 0;}
        else if (str1.compareTo(str2) > 0) {return 1;}
        else {return -1;}
    }
}

通常,如果 Map 的内部状态不一致,它就会启动 "misbehaving"。对于 TreeMap,就像您的情况一样,这可能是由不稳定的比较器引起的,这意味着它并不总是 return 对于相同的输入值会产生相同的结果。

无法访问整个代码,因此很难查明原因,但要注意的一件事是,如果您在 sort 方法中初始创建后修改它,您的 TreeMap 会变得不一致。那是因为您的比较器依赖于映射在实例化时的状态,比较器看不到任何以后的更改。

firstKey()firstEntry() 方法不使用比较器进行检索,它们只是在支持 TreeMap 的二叉树中一直"left"。但是 get(key) 使用比较器在树中查找键,在您的情况下不能正常工作。

this answer to a similar question 的另一个可能原因是您的比较器不符合要求

sgn(compare(x, y)) == -sgn(compare(y, x))

也就是说,如果两个条目 A 和 B 具有相同的值,则将 (A,B) 与您的代码进行比较得到 1,并且将 (B,A) 进行比较也得到 1,因此这 2 个元素的顺序没有正确定义。您还应该处理

的情况
map.get(s1).equals(map.get(s2))

比较两个条目和 return 0 时。请注意 equals 而不是 == 的用法,因为您不想比较两个 Double 对象参考,但按值代替。

对您现有的比较器实现进行了一些更改,以遵守比较器实现的约定:

  • 将通用参数从字符串替换为对象(不确定是否真的需要)
  • 键入 map.get(s1) 的转换值并获取双精度值进行比较
  • 分隔 >, < & ==

它似乎可以使用比较器的以下实现。

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

    public ValueComparator(HashMap<String, Double> map){
        this.map.putAll(map);
    }

    @Override
    public int compare(Object s1, Object s2) {
        if(((Double) map.get(s1)).doubleValue() > ((Double) map.get(s2)).doubleValue()){
            return 1;
        }
        else if (((Double) map.get(s1)).doubleValue() == ((Double) map.get(s2)).doubleValue()){
    return ((String)s1).compareTo(((String)s2));
         }
         else{
            return -1;
        }   
    }

对于以下示例值:

treemap.put( "two",2.0);
   treemap.put( "one",1.0);
   treemap.put( "three",3.0);
   treemap.put( "six",6.0);
   treemap.put( "five",5.0);

输出:

First key is: one
Value against first key: 1.0