我有这个代码(下面),当我打印树形图时,我可以清楚地看到键值对。每个键都有一个值(输出中没有空值)。当我得到第一个密钥时,它会给我密钥,但是当我尝试根据密钥获取值时,它将返回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()(我这样做) 但是当我去检索与该键相关联的值时,它返回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();
但我不知道为什么会这样,而另一种方法不起作用。 虽然我的代码现在有效,但我希望我能找出它的不同之处!
答案 0 :(得分:2)
如果地图内部状态不一致,通常地图就会“行为不端”。使用TreeMap,就像你的情况一样,这是由一个不稳定的比较器引起的,这意味着它并不总是为相同的输入值返回相同的结果。
无法访问整个代码很难确定原因,但有一点需要注意的是,如果在sort方法中初始创建后修改了TreeMap,则它会变得不一致。那是因为你的比较器在实例化时依赖于地图的状态,比较器看不到任何后来的变化。
firstKey()
和firstEntry()
方法不使用比较器进行检索,它们只是在支持TreeMap的二叉树中一直“左”。但是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))
比较两个条目并返回0.注意equals
而不是==
的用法,因为您不希望通过引用比较两个Double
对象,而是通过值来代替
答案 1 :(得分:1)
抱歉无法在问题中发表评论,所以必须在此处发帖作为答案。也许您可以先尝试将整个字符串复制到get输入,看看是否仍然为null
Double topvalue = cosinesimilarityvalues.get(“article04_C9,article08_C12”);
我发现自定义Comparator的覆盖比较方法存在问题。以下复制您的问题
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;}
}
}
答案 2 :(得分:1)
对比较器的现有实施做了一些改动,以便遵守比较器实施合同:
map.get(s1)
&amp;的投射值获取双值进行比较似乎可以使用比较器的以下实现。
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