Java-反转输出的显示方式

Java-反转输出的显示方式,java,file-io,output,reverse,Java,File Io,Output,Reverse,这是我的代码: 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

这是我的代码:

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;
    }
}
它产生这个输出:

[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”包含多个学生的每一列(“学生人数”部分成绩)

我想阅读该文件,并为每个学生将给定学生的所有部分分数添加到最终分数,因此我最终为每个学生添加(“学生人数”最终分数

然后我想按期末成绩的降序排列,如果学生的期末成绩相同,则按期末学生人数的降序排列。一些更正:

  • 由于您将该键用作辅助索引,因此还应将其转换为整数

  • 由于您希望使用键作为二级排序值,因此应分别更改比较函数中的最后一行(参见下面的示例)

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


  • 一些更正:

  • 由于您将该键用作辅助索引,因此还应将其转换为整数

  • 由于您希望使用键作为二级排序值,因此应分别更改比较函数中的最后一行(参见下面的示例)

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



  • 您可以通过返回res==0来更改可比到其他方式?物件:1@DreadfulWeather没有儿子,我已经试过了,它给了我这个输出:[10885=2060000=8,60001=5,70000=20]你也可以使用集合。相反,但我认为它不会有效,所以你应该尝试改变这个比较,你可以改变你的可比性,以其他方式返回res==0?物件:1@DreadfulWeather没有儿子,我已经试过了,它给了我这个输出:[10885=2060000=8,60001=5,70000=20]你也可以使用集合。相反,但我不认为它会有效,所以你应该尝试改变这个比较器。就是这样!非常感谢。我为我在另一条评论中的语言道歉。我同意这是粗鲁和不必要的,因为@可怕天气只是想帮助这一事业。我添加了你的智慧建议的传统信息(晚了一点),当我完成编辑时,我看到了你的答案,这正是我想要的。干杯就这样!非常感谢。我为我在另一条评论中的语言道歉。我同意这是粗鲁和不必要的,因为@可怕天气只是想帮助这一事业。我添加了你的智慧建议的传统信息(晚了一点),当我完成编辑时,我看到了你的答案,这正是我想要的。干杯
    [70000=20, 10885=20, 60003=8, 60002=8, 60000=8, 60001=5]
    
    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]