【问题标题】:How To Sort Object based on Object's attribute?如何根据对象的属性对对象进行排序?
【发布时间】:2012-12-08 01:51:47
【问题描述】:

除了排序方法之外,我得到了所有其他的东西。我需要根据学生的第一个属性对 HashMap 中的学生进行排序。我需要在将所有学生添加到 HashMap 之后发生排序方法,而不是在添加时发生。

package HashMap;

        import java.io.InputStream;
        import java.util.HashMap;
        import java.util.HashSet;
        import java.util.Scanner;

        public class clubMapping {
            HashMap<String, HashSet<Student>> map = new HashMap<String, HashSet<Student>>();

            public clubMapping(String clubName) {
                InputStream is = getClass().getClassLoader().getResourceAsStream(
                        "student.txt");
                Scanner scan = new Scanner(is);
                while (scan.hasNext())
                    add(scan.next(), scan.next(), Integer.parseInt(scan.next()),
                            scan.next());
                scan.close();
                System.out.println(map);
                System.out.println();
                System.out.println(map.get(clubName));
            }

            public void add(String last, String first, Integer id, String club) {
                HashSet<Student> set = new HashSet<Student>();
                if (!map.containsKey(club)) {
                    set.add(new Student(last, first, id));
                    map.put(club, set);
                } else {
                    set = map.get(club);
                    set.add(new Student(last, first, id));
                }
            }

            public static void main(String[] args) {
                new clubMapping("Math");
            }
        }

    package HashMap;

    public class Student {
        String last, first;
        Integer id;

        public Student(String l, String f, Integer i) {
            last = l;
            first = f;
            id = i;
        }

        public String toString() {
            return last + " " + first + " " + id;
        }
    }

【问题讨论】:

  • 能否缩短它以查明错误?
  • 我目前的代码没有错误,我只需要添加排序方法

标签: java hashmap


【解决方案1】:

对于学生集,您应该使用 TreeSet 而不是 HashSet。树集是隐式排序的,哈希集不是。

public void add(String last, String first, Integer id, String club) {
    TreeSet<Student> set = new TreeSet<Student>(studentComp);
    if (!map.containsKey(club)) {
        set.add(new Student(last, first, id));
        map.put(club, set);
    } else {
        set = map.get(club);
        set.add(new Student(last, first, id));
    }
}

但是你必须定义一个比较器来进行排序,如下所示。

private static Comparator<Student> studentComp = new Comparator<Student>() {
    @Override
    public int compare(Student s1, Student s2) {
        return (s1.last.compareTo(s2.last));
    }
};

此外,您可能应该将学生属性封装在 getter/setter 方法中,而不是像我在上面所做的那样直接访问它们(您的班级目前允许)。

【讨论】:

  • add方法中的studentcomp是什么
  • 它是一个比较器。 TreeSet 在排序时使用它来比较学生。
  • 这是一个自定义比较器,作为参数提供给TreeSet 构造函数。这将确保添加到集合中的所有内容都按比较器定义(在本例中为学生姓氏)排序。您说您不想在 添加所有学生之后之前进行排序,但这没有任何意义,因为您现在使用的 HashSet 无论如何都不会维护任何顺序,因此您不会因为维护而失去任何东西从一开始就排序。
  • 真的吗?我做了很多次后,它工作正常。但是,上面的帖子并不意味着要以一种或另一种方式排序,只是为了说明如何定义它。
  • @Prasanth 在此处查看TreeSet 的构造函数:docs.oracle.com/javase/7/docs/api/java/util/…
猜你喜欢
  • 1970-01-01
  • 2022-01-11
  • 2022-01-05
  • 1970-01-01
相关资源
最近更新 更多