0

在这个程序中它没有识别compartor,我不想使用compareTo.

class TestTreeSet
{
    public static void main(String args[])
    {
        BuildObject build = new BuildObject();
        TreeSet treeSet = new TreeSet( new Compared());
        treeSet = build.sendAsTreeSet();
        Iterator itr = treeSet.iterator();
        while(itr.hasNext())
        {
            Obj object = (Obj) itr.next();
            System.out.println(object.getName() + "\n " + object.getAge() + "\n "
                 + object.getSalary());
        }
    }
}

这是比较器

class Compared implements Comparator
{
    public int compare(Object a , Object b)
    {
        Obj one = (Obj) a;
        Obj two = (Obj) b;
        double salaryOne = one.getSalary();
        double salaryTwo = two.getSalary();
        int ageOne = one.getAge();
        int ageTwo = two.getAge();
        if( ageOne == ageTwo)
        {
            if(salaryOne > salaryTwo)
            {
                return -1;
            }
            else if(salaryOne < salaryTwo)
            {
                return 1;
            }
            else return 0;
        }
        else if(ageOne > ageTwo)
        return -1;
        else return 1;
    }
}

问题是什么 ?它显示不能转换为java.lang.comparable异常

4

5 回答 5

1

文档中:

基于 TreeMap 的 NavigableSet 实现。根据使用的构造函数,元素使用其自然顺序或在集合创建时提供的 Comparator 进行排序。

强调我的。

您可能认为您通过了 a Comparator,因此不使用自然排序。但在这一行

treeSet = build.sendAsTreeSet();

你覆盖treeSet你刚刚在上一行创建的。该树集很可能没有Comparator集合。

有两种方法可以解决这个问题:

  1. 确保由返回的树集build有一个Comparator集合(根据您的评论,new TreeSet(new Compared())在该方法中使用)。
  2. 确保树集中的元素由 implementbuild返回Comparable
于 2013-07-31T09:36:40.547 回答
1

既然你用

TreeSet treeSet = new TreeSet();

在您的sendAsTreeSet方法中,树集尝试使用自然排序对添加的元素进行排序。Witch 编译对象的类必须实现Compareable接口。

您应该将该行替换为

TreeSet treeSet = new TreeSet( new Compared());

可以删除 main 方法中的同一行。

于 2013-07-31T09:49:16.193 回答
0

我想这是

implements Comparable

所以只需要将其更改为正确的界面

于 2013-07-31T09:23:24.780 回答
0
BuildObject build = new BuildObject();
TreeSet treeSet = new TreeSet( new Compared());
treeSet = build.sendAsTreeSet();

如果方法返回包含类对象的sendAsTreeSetsa ,则可以更改如下,我希望它有效。TreeSetObjComparedClass

class Compared implements Comparator
{
public int compare(Obj a , Obj b)
{

    double salaryOne = a.getSalary();
    double salaryTwo = b.getSalary();
    int ageOne = a.getAge();
    int ageTwo = b.getAge();
    if( ageOne == ageTwo)
    {
        if(salaryOne > salaryTwo)
        {
            return -1;
        }
        else if(salaryOne < salaryTwo)
        {
            return 1;
        }
        else return 0;
    }
    else if(ageOne > ageTwo)
    return -1;
    else return 1;
}
}
于 2013-07-31T09:44:30.020 回答
0

当您将集合覆盖或添加到集合(树集)时,您需要您的元素或类通过稍后集合中使用的比较器实现可比较性。

尝试在您的元素类上实现可比较,如下所示

class Person implements Comparable{

  @Override
  public int compareTo(Object arg0) {
    return -1;
  }
}

而是通过分配参考

treeSet = build.sendAsTreeSet(); 

使用 addAll 如下

treeSet.addAll(build.sendAsTreeSet()); 

希望对你有用。请让我知道它是否不起作用

检查下面的样本

public class ComparatorTest {
    public static void main(String[] args) {

        TreeSet<Person> treeSet = new TreeSet<Person>(new Comparator());
        treeSet.addAll(new BuildObject().sendAsTreeSet());
        Iterator<Person> itr = treeSet.iterator();
        while(itr.hasNext())
        {
            Person itrObject =  itr.next();
            System.out.println("Name: "+itrObject.getName()+"\n Age: "+itrObject.getAge() + "\n Salary: " + itrObject.getSalary());
        }
    }
}

class Comparator implements java.util.Comparator<Person> {
    @Override
    public int compare(Person one, Person two) {
        double salaryOne = one.getSalary();
        double salaryTwo = two.getSalary();
        int ageOne = one.getAge();
        int ageTwo = two.getAge();
        if (ageOne == ageTwo) {
            if (salaryOne > salaryTwo) {
                return -1;
            } else if (salaryOne < salaryTwo) {
                return 1;
            } else
                return 0;
        } else if (ageOne > ageTwo)
            return -1;
        else
            return 1;
    }
}

class BuildObject{
    public TreeSet<Person> sendAsTreeSet(){
        Person object = new Person();
        object.setName("Pritesh");
        object.setAge(20);
        object.setSalary(1000);

        Person object2 = new Person();
        object2.setName("Joe");
        object2.setAge(19);
        object2.setSalary(3000);

        Person object3 = new Person();
        object3.setName("Blogg");
        object3.setAge(20);
        object3.setSalary(4000);

        TreeSet<Person> treeSet =  new TreeSet<Person>();
        treeSet.add(object);
        treeSet.add(object2);
        treeSet.add(object3);
        return treeSet;
    }
}

class Person implements Comparable{
    private String name;
    private int age;
    private int salary;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    @Override
    public int compareTo(Object arg0) {
        return -1;
    }
}
于 2013-07-31T10:02:53.450 回答