英文:
How to sort a List of String according to a Double score that is stored?
问题
这是我的问题:我必须显示一组嵌套结构,并且我有一个问题。我应该将一些查询写入文件,带有关联的结果文件,查询在文件中出现的次数,以及计算出的分数。唯一的问题是我必须按分数而不是按字母顺序对显示的结果进行排序(这是我现在正在做的)。我不知道该使用什么或从哪里开始,所以如果我能得到一些帮助,我会非常高兴。这是函数:
public static void asNestedArrayQueries(List<String> elements, TreeMap<String, TreeMap<String, TreeSet<Integer>>> actual, TreeMap<String, Integer> countMap, Writer writer, int level)
throws IOException {
TreeMap<String, Integer> resultMap = new TreeMap<String, Integer>();
int counter = 0, counterOfFiles = 0;
double divider = 0;
elements.sort(Comparator.comparing(String::toString));
writer.write("{\n");
for (int i = 0; i < elements.size(); i++) {
counter++;
indent(writer, 1);
writer.write("\"" + elements.get(i) + "\": [\n");
String[] words = elements.get(i).split(" ");
counterOfFiles = 0;
for (int j = 0; j < words.length; j++) {
if (actual.containsKey(words[j])) {
for (String keyTwo : actual.get(words[j]).keySet()) {
if (!resultMap.containsKey(keyTwo)) {
resultMap.put(keyTwo, actual.get(words[j]).get(keyTwo).size());
} else if (resultMap.containsKey(keyTwo)) {
resultMap.put(keyTwo, resultMap.get(keyTwo) + actual.get(words[j]).get(keyTwo).size());
}
System.out.println(resultMap);
divider = countMap.get(keyTwo);
}
}
}
for (String key : resultMap.keySet()) {
double score = resultMap.get(key) / divider;
String formatted;
counterOfFiles++;
indent(writer, 2);
writer.write("{\n");
indent(writer, 3);
writer.write("\"where\": \"" + key + "\",\n");
indent(writer, 3);
writer.write("\"count\": " + resultMap.get(key) + ",\n");
indent(writer, 3);
formatted = String.format(Locale.US, "%.8f", score);
writer.write("\"score\": " + formatted + "\n");
if (counterOfFiles < resultMap.keySet().size()) {
indent(writer, 2);
writer.write("},\n");
} else {
indent(writer, 2);
writer.write("}\n");
}
}
if (counter < elements.size()) {
indent(writer, 1);
writer.write("],\n");
} else {
indent(writer, 1);
writer.write("]\n");
}
resultMap.clear();
}
writer.write("}");
elements.clear();
}
英文:
Here is my problem: I have to display a set of Nested structures and I have a problem. I am supposed to write to a file a queries, with associated result file, number of time the query appears in the file, and a score that is calculated. Only problem is that I have to sort the displayed result by score and not by alphabetical order ( which is what I am doing right now ). I have no idea what to use or where to start so I'd be incredibly happy if I could get some help. Here is the function:
public static void asNestedArrayQueries(List<String> elements, TreeMap<String, TreeMap<String, TreeSet<Integer>>> actual, TreeMap<String, Integer> countMap, Writer writer, int level)
throws IOException {
TreeMap<String, Integer> resultMap = new TreeMap< String, Integer>();
int counter=0, counterOfFiles=0;
double divider=0;
elements.sort( Comparator.comparing( String::toString ) );
writer.write("{\n");
for(int i=0;i<elements.size();i++) {
counter++;
indent(writer, 1);
writer.write("\"" + elements.get(i) + "\": [\n");
String[] words = elements.get(i).split(" ");
counterOfFiles=0;
for(int j=0;j<words.length;j++) {
if(actual.containsKey(words[j])){
for(String keyTwo : actual.get(words[j]).keySet()) {
if(!resultMap.containsKey(keyTwo)) {
resultMap.put(keyTwo, actual.get(words[j]).get(keyTwo).size());
}
else if(resultMap.containsKey(keyTwo)){
resultMap.put(keyTwo, resultMap.get(keyTwo)+actual.get(words[j]).get(keyTwo).size());
}
System.out.println(resultMap);
divider = countMap.get(keyTwo);
}
}
}
for(String key : resultMap.keySet()) {
double score = resultMap.get(key)/divider;
String formatted;
counterOfFiles++;
indent(writer, 2);
writer.write("{\n");
indent(writer, 3);
writer.write("\"where\": \"" + key + "\",\n");
indent(writer, 3);
writer.write("\"count\": " + resultMap.get(key) + ",\n");
indent(writer, 3);
formatted = String.format(Locale.US, "%.8f", score);
writer.write("\"score\": " + formatted + "\n");
if(counterOfFiles<resultMap.keySet().size()) {
indent(writer, 2);
writer.write("},\n");
}
else {
indent(writer, 2);
writer.write("}\n");
}
}
if(counter<elements.size()) {
indent(writer, 1);
writer.write("],\n");
}
else {
indent(writer, 1);
writer.write("]\n");
}
resultMap.clear();
}
writer.write("}");
elements.clear();
}
答案1
得分: 0
我正在对Map进行排序,但也适用于TreeMap。您可以通过创建自定义比较器按值对其进行排序 -> Collections.sort(list, customComparator)。
这里有一个简单Map的示例,其中包含一个键和一个值。是的,这里使用的值是String,但是对于您的分数,也可以是Integer:
Map<Integer, String> firstMap = new ConcurrentHashMap<>();
Map<Integer, String> secondMap = new ConcurrentHashMap<>();
firstMap.put(1, "name");
firstMap.put(2, "year");
firstMap.put(3, "month");
firstMap.put(4, "day");
secondMap = sortByComparator(firstMap, true);
该方法:
/**
* 此方法按顺序对ConcurrentHashMap变量进行排序
* @param unsortedMap - 要排序的ConcurrentHashMap
* @param order - true表示降序,false表示升序
* @return 排序后的ConcurrentHashMap变量
*/
private static Map<Integer, String> sortByComparator(Map<Integer, String> unsortedMap, final boolean order) {
List<Map.Entry<Integer, String>> list = new LinkedList<Map.Entry<Integer, String>>(unsortedMap.entrySet());
// 基于值通过自定义比较器对列表进行排序
Collections.sort(list, new Comparator<Map.Entry<Integer, String>>() {
public int compare(Map.Entry<Integer, String> o1,
Map.Entry<Integer, String> o2) {
if (order) {
return o1.getValue().compareTo(o2.getValue());
} else {
return o2.getValue().compareTo(o1.getValue());
}
}
});
// 借助LinkedList维护插入顺序
Map<Integer, String> sortedMap = new LinkedHashMap<Integer, String>();
for (Map.Entry<Integer, String> entry : list) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
英文:
I am sorting Map but it may work for TreeMap as well. You can sort it by value via creating custom Comparator -> Collections.sort(list, customComparator) .
Here is example for a simple Map with one Key and one Value. Yes the Value used here is String, but it can be Integer as well for your score:
Map<Integer, String> firstMap = new ConcurrentHashMap<>();
Map<Integer, String> secondMap = new ConcurrentHashMap<>();
firstMap.put(1, "name");
firstMap.put(2, "year");
firstMap.put(3, "month");
firstMap.put(4, "day");
secondMap = sortByComparator(firstMap, true);
the method:
/**
* This method sorts ConcurrentHashMap variable by order
* @param unsortedMap - the ConcurrentHashMap to sort
* @param order - true for descending order, false for ascending
* @return sorted ConcurrentHashMap variable
*/
private static Map<Integer, String> sortByComparator(Map<Integer, String> unsortedMap, final boolean order) {
List<Map.Entry<Integer, String>> list = new LinkedList<Map.Entry<Integer, String>>(unsortedMap.entrySet());
// Sorting the list based on values via CUSTOM COMPARATOR
Collections.sort(list, new Comparator<Map.Entry<Integer, String>>() {
public int compare(Map.Entry<Integer, String> o1,
Map.Entry<Integer, String> o2) {
if (order) {
return o1.getValue().compareTo(o2.getValue());
} else {
return o2.getValue().compareTo(o1.getValue());
}
}
});
// Maintaining insertion order with the help of LinkedList
Map<Integer, String> sortedMap = new LinkedHashMap<Integer, String>();
for (Map.Entry<Integer, String> entry : list) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论