如何在Java中使用Comparator进行排序
我学会了如何使用可比对象,但是在使用比较器时遇到了困难。我的代码有错误:
Exception in thread "main" java.lang.ClassCastException: New.People cannot be cast to java.lang.Comparable at java.util.Arrays.mergeSort(Unknown Source)
 at java.util.Arrays.sort(Unknown Source)
 at java.util.Collections.sort(Unknown Source)
 at New.TestPeople.main(TestPeople.java:18)
这是我的代码:
import java.util.Comparator;public class People implements Comparator {
   private int id;
   private String info;
   private double price;
   public People(int newid, String newinfo, double newprice) {
       setid(newid);
       setinfo(newinfo);
       setprice(newprice);
   }
   public int getid() {
       return id;
   }
   public void setid(int id) {
       this.id = id;
   }
   public String getinfo() {
       return info;
   }
   public void setinfo(String info) {
       this.info = info;
   }
   public double getprice() {
       return price;
   }
   public void setprice(double price) {
       this.price = price;
   }
   public int compare(Object obj1, Object obj2) {
       Integer p1 = ((People) obj1).getid();
       Integer p2 = ((People) obj2).getid();
       if (p1 > p2) {
           return 1;
       } else if (p1 < p2){
           return -1;
       } else {
           return 0;
       }
    }
}
import java.util.ArrayList;import java.util.Collections;
public class TestPeople {
    public static void main(String[] args) {
        ArrayList peps = new ArrayList();
        peps.add(new People(123, "M", 14.25));
        peps.add(new People(234, "M", 6.21));
        peps.add(new People(362, "F", 9.23));
        peps.add(new People(111, "M", 65.99));
        peps.add(new People(535, "F", 9.23));
        Collections.sort(peps);
        for (int i = 0; i < peps.size(); i++){
            System.out.println(peps.get(i));
        }
    }
}
我相信它必须与比较方法中的转换有关,但我一直在玩,但仍然找不到解决方案
回答:
你的示例类有几件尴尬的事情:
- 它有个price和时称为人info(更多用于对象,而不是人);
- 当将一个类命名为某个事物的复数形式时,这表明它是对多个事物的抽象。
无论如何,这是如何使用的演示Comparator<T>:
public class ComparatorDemo {    public static void main(String[] args) {
        List<Person> people = Arrays.asList(
                new Person("Joe", 24),
                new Person("Pete", 18),
                new Person("Chris", 21)
        );
        Collections.sort(people, new LexicographicComparator());
        System.out.println(people);
        Collections.sort(people, new AgeComparator());
        System.out.println(people);
    }
}
class LexicographicComparator implements Comparator<Person> {
    @Override
    public int compare(Person a, Person b) {
        return a.name.compareToIgnoreCase(b.name);
    }
}
class AgeComparator implements Comparator<Person> {
    @Override
    public int compare(Person a, Person b) {
        return a.age < b.age ? -1 : a.age == b.age ? 0 : 1;
    }
}
class Person {
    String name;
    int age;
    Person(String n, int a) {
        name = n;
        age = a;
    }
    @Override
    public String toString() {
        return String.format("{name=%s, age=%d}", name, age);
    }
}
编辑
等效的Java 8演示如下所示:
public class ComparatorDemo {    public static void main(String[] args) {
        List<Person> people = Arrays.asList(
                new Person("Joe", 24),
                new Person("Pete", 18),
                new Person("Chris", 21)
        );
        Collections.sort(people, (a, b) -> a.name.compareToIgnoreCase(b.name));
        System.out.println(people);
        Collections.sort(people, (a, b) -> a.age < b.age ? -1 : a.age == b.age ? 0 : 1);
        System.out.println(people);
    }
}
以上是 如何在Java中使用Comparator进行排序 的全部内容, 来源链接: utcz.com/qa/415395.html








