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如何使文本文件在排序后也能与相似度分数合并?在另一种意义上,即使在排序之后,输出也会显示每个单独的文本文件跟随其单独的分数。
发布于 2011-03-27 01:02:56
听起来最简单(或最直接)的方法是使用一个集合和一个比较器。
将这些值放入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));
}https://stackoverflow.com/questions/5443801
复制相似问题