I have this code (below) that when I print the treemap, I can clearly see the key,value pairs. Every key has a value (no null values in the output). When I get the first key, it will give me the key, but when I try to get the value based on the key, it will 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);
Here is part of an output :
{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}
So I should be getting article04_C9,article08_C12 as firstKey() (which I do) but when I go to retrieve the value associated with that key, it returns null.
Here is the code that I'm using to populate the treemap
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;
}
Here is the function I am using to sort by value...if that helps
from : 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;
}
I'm not sure what I'm doing wrong. Please help!
Thanks!
UPDATE
I was able to retrieve the top key and top value via
Map.Entry<String, Double> entry1 = cosinesimilarityvalues.firstEntry();
String topkey = entry1.getKey();
Double topvalue = entry1.getValue();
but I have no idea why this works and the other method does not work. Although my code now works, I wish I could find out what the difference is!