[英]not able to use the comparator in my program
在这个程序中它没有识别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
异常
从文档 :
基于TreeMap的NavigableSet实现。 元素按照其自然顺序排序 ,或者在创建时创建时提供的比较器,具体取决于使用的构造函数。
强调我的。
您可能认为通过了Comparator
,因此不使用自然顺序。 但在这一行
treeSet = build.sendAsTreeSet();
覆盖刚刚在上一行中创建的treeSet
。 该树集很可能没有Comparator
集。
有两种方法可以解决这个问题:
build
具有Comparator
集(根据您的意见,使用new TreeSet(new Compared())
该方法)。 build
返回的树集中的元素实现Comparable
。 既然你用了
TreeSet treeSet = new TreeSet();
在您的sendAsTreeSet
方法中,树集尝试使用自然顺序对添加的元素进行排序 。 Witch对你的对象类进行了侮辱,实现了Compareable接口。
你应该用那个替换那一行
TreeSet treeSet = new TreeSet( new Compared());
可以删除main方法中的相同行。
我想这是
implements Comparable
所以只需将其更改为正确的界面即可
BuildObject build = new BuildObject();
TreeSet treeSet = new TreeSet( new Compared());
treeSet = build.sendAsTreeSet();
如果sendAsTreeSet
方法返回SA TreeSet
包含类型的对象Obj
类,那么,你可以改变ComparedClass
如下,我希望它的作品。
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;
}
}
当您将集合覆盖或添加到集合(Treeset)时,您需要使用后续集合中使用的比较器来实现可比较的元素或类。
尝试在元素类上实现可比较,如下所示
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.