Java - 反转输出的显示方式

Java - Reversing the way the output is shown

这是我的代码:

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. 既然要倒序排序,比较函数应该return相反的结果,即结果前的-(负号)


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]