gpt4 book ai didi

Java - 反转输出的显示方式

转载 作者:太空宇宙 更新时间:2023-11-04 13:27:08 25 4
gpt4 key购买 nike

这是我的代码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;

/**
* Created by Luis on 09/09/2015.
*/
public class ReadToHashmap {
public static void main(String[] args) throws Exception {
Map<String, Integer> mapofstuff = new TreeMap<String, Integer>();
BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\Luis\\Desktop\\Java.POO\\testingide\\src\\la2\\grades.txt"));
String line = "";
while ((line = in.readLine()) != null) {
String parts[] = line.split(" ", 2);

int part1 = Integer.parseInt(parts[1]);

if(mapofstuff.containsKey(parts[0])) {
mapofstuff.put(parts[0], mapofstuff.get(parts[0])+part1);
}

else {
mapofstuff.put(parts[0], part1);
}
}
in.close();


System.out.println(entriesSortedByValues(mapofstuff));
}


public static <K,V extends Comparable<? super V>>
SortedSet<Map.Entry<K,V>> entriesSortedByValues(Map<K,V> map) {
SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<Map.Entry<K,V>>(
new Comparator<Map.Entry<K,V>>() {
@Override public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {
int res = e1.getValue().compareTo(e2.getValue());
return res != 0 ? res : 1;
}
}
);
sortedEntries.addAll(map.entrySet());
return sortedEntries;
}
}

这是文件“grades.txt”包含的内容:

10885 10
70000 6
70000 10
60000 4
70000 4
60000 4
10885 10
60001 5
60002 8
60003 8

它会产生以下输出:

[60001=5, 60000=8, 60002=8, 60003=8, 10885=20, 70000=20]

这是我想要的输出:

[70000=20, 10885=20, 60003=8, 60002=8, 60000=8, 60001=5]

有效地反转我目前的输出。感谢您抽出时间。

其他信息:

“grades.txt” 包含多个学生的每列(“Number_of_student”partial_grade)。

我想要读取该文件,并将每个学生的所有部分成绩添加到最终成绩中,因此我最终会得到每个学生的 ("Number_of_student", final_grade)。

然后我想按final_grade降序排列如果学生有相同的final_grade,则按Number_of_student降序排列

最佳答案

一些更正:

  1. 由于您使用该键作为辅助索引,因此您也应该将其转换为整数。

  2. 由于要使用key作为辅助排序值,因此比较函数中的最后一行应分别更改(见下面的示例)

  3. 由于要按相反顺序排序,比较函数应返回相反的结果,即结果之前的 -(减号)。

<小时/>
public static void main(String[] args) throws Exception {
Map<Integer, Integer> mapofstuff = new TreeMap<>();
BufferedReader in = new BufferedReader(new FileReader("C:\Users\Luis\Desktop\Java.POO\testingide\src\la2\grades.txt"));
String line = "";
while ((line = in.readLine()) != null) {
String parts[] = line.split(" ", 2);

int part0 = Integer.parseInt(parts[0].trim());
int part1 = Integer.parseInt(parts[1].trim());

if(mapofstuff.containsKey(part0)) {
mapofstuff.put(part0, mapofstuff.get(part0) + part1);
}

else {
mapofstuff.put(part0, part1);
}
}
in.close();


System.out.println(entriesSortedByValues(mapofstuff));
}


public static <K,V extends Comparable<? super V>>
SortedSet<Map.Entry<K,V>> entriesSortedByValues(Map<K,V> map) {
SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<Map.Entry<K,V>>(
new Comparator<Map.Entry<K,V>>() {
@Override public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {
int res = -e1.getValue().compareTo(e2.getValue());
return res != 0 ? res : -((Integer)e1.getKey()).compareTo((Integer)e2.getKey());
}
}
);
sortedEntries.addAll(map.entrySet());
return sortedEntries;
}

输出

[70000=20, 10885=20, 60003=8, 60002=8, 60000=8, 60001=5]

关于Java - 反转输出的显示方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32534505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com