for (a = 0; a < filename; a++) {
Map<Double,String> m = new HashMap<Double,String>();
String pre = "abc";
String post = ".txt";
for (int ii = 0; ii < 11; ii++) {
m.put(similarityScore[a],pre + a + post + '\n');
}
SortedSet<Double> set = new TreeSet<Double>(m.keySet());
for (Double d : set) {
System.out.println(d + " " + m.get(d));
}
}
示例输出:
The resulting similarity score of the query how [INITIAL OUTPUT]
abc0.txt = 0.5773502691896258
abc1.txt = 0.5773502691896258
abc2.txt = 0.5773502691896258
abc3.txt = NaN
abc4.txt = 0.5773502691896258
abc5.txt = NaN
abc6.txt = NaN
abc7.txt = NaN
abc8.txt = NaN
abc9.txt = 0.5773502691896258
abc10.txt = NaN
Similarity score sorted **DESIRED** output :
0.5773502691896258 abc0.txt
0.5773502691896258 abc1.txt
0.5773502691896258 abc2.txt
0.5773502691896258 abc4.txt
0.5773502691896258 abc9.txt
NaN abc3.txt
NaN abc5.txt
NaN abc6.txt
NaN abc7.txt
NaN abc8.txt
NaN abc10.txt
我如何使文本文件与相似性得分合并,甚至排序?在另一种意义上,即使在排序之后,输出显示每个单独的文本文件也遵循其个体分数。
答案 0 :(得分:0)
使用集合和比较器听起来最简单(或最直接)。
将值放在Map中,然后对double进行排序。这甚至可能是自然排序顺序,因此您不必编写比较器。
Map<Double,String> m = new HashMap<Double,String>();
String pre = "abc";
String post = ".txt";
for (int ii = 0; ii < 10; ii++) {
m.put(Math.random(),pre + ii + post);
}
SortedSet<Double> set = new TreeSet<Double>(m.keySet());
for (Double d : set) {
System.out.println(d + " " + m.get(d));
}
这将返回一个已排序的输出(从低到高),您可以轻松地将其反转或以不同的方式运行。
- 编辑以使用OP的修改代码显示:
// Initialize variables
String pre = "abc";
String post = ".txt";
Map<Double,String> m = new HashMap<Double,String>();
// Add data to your map
for (int a = 0; a < filename; a++) {
m.put(similarityScore[a],pre + a + post + '\n');
}
// sort the map keyset and print out the sorted results
SortedSet<Double> set = new TreeSet<Double>(m.keySet());
for (int ii = set.size(); ii >= 0; --ii) {
System.out.println(set.get(ii) + " " + m.get(d));
}