如何根据对象的属性对对象进行排序?

编程入门 行业动态 更新时间:2024-10-27 04:36:55
本文介绍了如何根据对象的属性对对象进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 除了排序方法外,我还有其他所有工作。我需要根据学生的第一个属性对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>>(); $ b public clubMapping(String clubName){ InputStream is = getClass()。getClassLoader()。getResourceAsStream(student.txt); 扫描仪扫描=新扫描仪(是); ()扫描.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); } } 软件包HashMap; 公共班级学生{字符串最后,第一个; 整数ID; public Student(String l,String f,Integer i){ last = l; first = f; id = i; } public String toString(){ return last ++ first ++ id; $ div $解析方案

你应该为学生组使用 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)); $ b

但是你必须定义一个用于排序的比较器,如下所示。

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

另外,您应该将您的学生属性封装在getter / setter方法中,而不是直接以我已经完成了上面的内容(正如你的班级目前允许的那样)。

I got everything else to work except the sort method. I need to sort the Students in the HashMap based on the Student's first attribute. I need the sort method to happen after I added all of the students in the HashMap, not while it's being added.

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; } }

解决方案

You should use a TreeSet instead of a HashSet for the student set. A tree set is implicitly sorted, a hash set is not.

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)); } }

But you must define a comparator to use for the sorting, as shown below.

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

Also, you should probably encapsulate your student attributes in getter/setter methods, rather than accessing them directly as I've done above (as your class currently allows).

更多推荐

如何根据对象的属性对对象进行排序?

本文发布于:2023-11-30 05:49:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648929.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   属性

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!