Returning a SortedSet [closed]

情到浓时终转凉″ 提交于 2019-12-12 03:25:33

问题


When I run the following code:

  Student student1 = new Student("Billy", 13);
  Student student2 = new Student("Bob", 12);
  Student student3 = new Student("Belle", 11);
  Student student4 = new Student("Barry", 10);
  Student student5 = new Student("Brian", 10);
  Student student6 = new Student("Bane", 13);
  Collection<Student> students = new HashSet<Student>();
  students.add(student1);
  students.add(student2);
  students.add(student3);
  students.add(student4);
  students.add(student5);
  students.add(student6);
  for(Student student : students)
  {
    String name = student.getName();
    System.out.println(name);
  }

It will print out a list of names for my student objects. Now I'd like to do them in alphabetical order. I thought it would be as simple as using a TreeSet or SortedSet.

Like this:

 Student student1 = new Student("Billy", 13);
 Student student2 = new Student("Bob", 12);
 Student student3 = new Student("Belle", 11);
 Student student4 = new Student("Barry", 10);
 Student student5 = new Student("Brian", 10);
 Student student6 = new Student("Bane", 13);
 Collection<Student> students = **new TreeSet<Student>();**
 students.add(student1);
 students.add(student2);
 students.add(student3);
 students.add(student4);
 students.add(student5);
 students.add(student6);
 for(Student student : students)
 {
    String name = student.getName();
    System.out.println(name);
 }

But this just throws the exception:

  Exception in thread "main" java.lang.ClassCastException: helloworld.Student cannot be cast to java.lang.Comparable
    at java.util.TreeMap.put(TreeMap.java:542)
    at java.util.TreeSet.add(TreeSet.java:238)
    at helloworld.Main.main(Main.java:60)

Java Result: 1

I have added a compareTo method in the student class too:

 public int compareTo(Student other)
 {
   return this.getName().compareTo(other.getName());
 }

回答1:


What do you mean by "order"? If you mean in the order they were added, then just use LinkedHashSet. If you want some kind of sorting, then you have to describe how Students should be sorted by having Student implement Comparable<Student> or by providing a Comparator<Student>.

If you meant alphabetical order, then you should modify the Student class like so:

class Student implements Comparable<Student> {
   ...
   public int compareTo(Student other) {
     return getName().compareTo(other.getName());
   }
}



回答2:


For Alphabetical order, you have 2 ways.

  1. Either alter your Student class by implementing comparable interface.

    Reason :- When we add element to TreeSet then for each existing object in Treeset JVM compares current object by calling compareTo/compare method on them and by implementing Comparable interface we are providing compareTo method implementaton.

  2. OR- Provide a Comparator object while creating TreeSet i.e

    Collection<Student> tm = new TreeSet<Student>(new Comparator<Student>() {
    
        @Override
        public int compare(Student o1, Student o2) {
            return o1.getName().compareTo(o2.getName());
        }
    
    });
    

    Here,I've used anonymous Comparator.Hope this Helps.



来源:https://stackoverflow.com/questions/28178337/returning-a-sortedset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!