简体   繁体   English

使用带有字符串和单独类的Map对集合进行排序

[英]Sorting collections using a Map with string and separate class

I am basically trying to sort an input file of Students and Marks into alphabetic and numeric order. 我基本上是试图按字母和数字顺序对学生和标记的输入文件进行排序。 I have 4 classes, however I cannot manage to get it to print the student with the mark in any order. 我有4个课程,但是我无法设法以任何顺序打印带有标记的学生。 Let alone in a alphabetic and numeric order. 更不用说按字母和数字的顺序了。 Any help in how I can get the results printing as a total or any help at all is greatly appreciated. 非常感谢您对我如何获得总体打印结果的帮助或任何帮助。 Below is the code I have used for the 4 classes and the input file. 下面是我用于4个类和输入文件的代码。

Input File: 输入文件:

Simon 4
Anna 10
Simon 4
Anna 9
Anna 5
Edward 10

Code: 码:

package part1;
import java.io.FileReader;
import java.util.*;
public class Main {
    public static void main(String[] args) throws Exception {
        Map<String, StudentMath> map = new HashMap<String, StudentMath>();
        String input = "data/results.txt";
        Scanner scan = new Scanner(new FileReader(input));
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            String[] in = line.split(" ");
            String name = in[0];
            int mark = Integer.parseInt(in[1]);
            //System.out.println(name + ":" + mark);
            StudentMath stud = map.get(name);
            if (stud == null) {
                stud = new StudentMath(name, mark);
                map.put(name, stud);
                stud.sum(mark);
            }

        }
        for (String s: map.keySet()){
            System.out.println(s);
        }
    }
}

package part1;

public class StudentMath extends Main {

    public String name;
    public int mark;

    public StudentMath(String s, int n) {
        name = s;
        mark = n;
    }

    public String getName() {
        return name;

    }
    public int getMark() {
        return mark;
    }

    public int sum() {
       int tot = mark + mark;
       return tot;
    }

    public boolean equals(Object o) {
        if (o instanceof StudentMath) {
            StudentMath m = (StudentMath) o;
            return (name == m.name) && (mark == m.mark);

        }
        else {
            return false;
        }
    }
}

package part1;

import java.util.Comparator;

public class NameCompare implements Comparator<StudentMath> {
    public int compare(StudentMath g1, StudentMath g2) {
        return g1.name.compareTo(g2.name);
    }
}


package part1;


import java.util.Comparator;

public class MarkCompare implements Comparator<StudentMath>{

    public int compare(StudentMath g1, StudentMath g2) {
        return g2.mark - g1.mark;
    }
}

A HashMap is an unordered collection, so it does not define an ordering over it's keys. HashMap无序集合,因此它没有定义其键的排序。 That is why you get your output in random order. 这就是为什么您以随机顺序获得输出的原因。

You could try using an ordered collection type, such as a List ( ArrayList or LinkedList ), or if you really need to have a Map then you could look at the TreeMap class. 您可以尝试使用有序的集合类型,例如ListArrayListLinkedList ),或者如果您确实需要Map那么可以查看TreeMap类。 TreeMap is a Map implementation that knows how to sort its keys. TreeMap是一个Map实现,它知道如何对其键进行排序。 Note that TreeMap is not able to sort by value, so it probably isn't the right data structure to use here because you won't be able to (easily) use it to sort by mark. 请注意, TreeMap无法按值排序,因此它可能不是在此处使用的正确数据结构,因为您将无法(轻松地)将其用于按标记排序。

I won't give you code because this is clearly homework, but hopefully that will get you on the right track. 我不会给您代码,因为这显然是家庭作业,但是希望可以使您走上正确的道路。

EDIT: To address the comment below: ordered is not the same as sorted . 编辑:解决以下评论: 排序sorted不同 To actually sort a List into the order you want, take a look at the Collections.sort methods. 要将List实际排序为所需的顺序,请查看Collections.sort方法。

Never ever compare Strings using "==" always use "equals" to compare. 永远不要使用“ ==”来比较字符串,始终使用“等于”来进行比较。 in the line 在行中

return (name == m.name) && (mark == m.mark);

change it to 更改为

return (name.equals(m.name))&& (mark == m.mark);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM