如何根据存储的双分数对字符串列表进行排序?
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();
}
我正在对 Map 进行排序,但它也可能适用于 TreeMap。您可以通过创建自定义 Comparator -> Collections.sort(list, customComparator) 按值对其进行排序。
下面是一个具有一个键和一个值的简单映射的示例。是的,这里使用的值是字符串,但对于您的分数也可以是整数:
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);
方法:
/**
* 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;
}
这是我的问题:我必须显示一组嵌套结构,但我遇到了问题。我应该将查询、关联的结果文件、查询在文件中出现的次数以及计算出的分数写入文件。唯一的问题是我必须按分数而不是按字母顺序对显示的结果进行排序(这是我现在正在做的)。我不知道该用什么或从哪里开始,所以如果我能得到一些帮助,我会非常高兴。这是函数:
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();
}
我正在对 Map 进行排序,但它也可能适用于 TreeMap。您可以通过创建自定义 Comparator -> Collections.sort(list, customComparator) 按值对其进行排序。 下面是一个具有一个键和一个值的简单映射的示例。是的,这里使用的值是字符串,但对于您的分数也可以是整数:
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);
方法:
/**
* 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;
}