[英]not able to use the comparator in my program
In this program it is not identifying the compartor
, I don't want to use the compareTo
. 在这个程序中它没有识别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());
}
}
}
this is the comparator 这是比较器
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;
}
}
what is the problem ? 问题是什么 ? it shows cannot be cast to java.lang.comparable
exception 它显示无法转换为java.lang.comparable
异常
From the documentation : 从文档 :
A NavigableSet implementation based on a TreeMap. 基于TreeMap的NavigableSet实现。 The elements are ordered using their natural ordering , or by a Comparator provided at set creation time, depending on which constructor is used. 元素按照其自然顺序排序 ,或者在创建时创建时提供的比较器,具体取决于使用的构造函数。
Emphasis mine. 强调我的。
You might think you pass a Comparator
, so the natural ordering is not used. 您可能认为通过了Comparator
,因此不使用自然顺序。 But in this line 但在这一行
treeSet = build.sendAsTreeSet();
you overwrite the treeSet
you just created in the previous line. 覆盖刚刚在上一行中创建的treeSet
。 That tree set most likely doesn't have a Comparator
set. 该树集很可能没有Comparator
集。
There are two ways to solve this: 有两种方法可以解决这个问题:
build
has a Comparator
set (based on your comment, use new TreeSet(new Compared())
in that method). 确保返回的TreeSet的build
具有Comparator
集(根据您的意见,使用new TreeSet(new Compared())
该方法)。 build
implement Comparable
. 确保build
返回的树集中的元素实现Comparable
。 Since you use 既然你用了
TreeSet treeSet = new TreeSet();
in your sendAsTreeSet
method, the treeset tries to sort the elements added using theire natural ordering . 在您的sendAsTreeSet
方法中,树集尝试使用自然顺序对添加的元素进行排序 。 Witch impiles the class of your objects musst implement the Compareable interface. Witch对你的对象类进行了侮辱,实现了Compareable接口。
You should replace that line with 你应该用那个替换那一行
TreeSet treeSet = new TreeSet( new Compared());
The same line in the main method can be removed. 可以删除main方法中的相同行。
I think its 我想这是
implements Comparable
So just have to change it to the correct interface 所以只需将其更改为正确的界面即可
BuildObject build = new BuildObject();
TreeSet treeSet = new TreeSet( new Compared());
treeSet = build.sendAsTreeSet();
If sendAsTreeSet
method return sa TreeSet
which contains object of type Obj
class then, you can change the ComparedClass
as following, i hope it works. 如果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;
}
}
When you are overriding or adding collection to collection(Treeset) you need your elements or class to implement comparable by comparator used in later collection. 当您将集合覆盖或添加到集合(Treeset)时,您需要使用后续集合中使用的比较器来实现可比较的元素或类。
Try implementing comparable on your element class as below 尝试在元素类上实现可比较,如下所示
class Person implements Comparable{
@Override
public int compareTo(Object arg0) {
return -1;
}
}
and instead to assigning reference by 而是通过分配引用
treeSet = build.sendAsTreeSet();
use addAll as below 使用addAll如下
treeSet.addAll(build.sendAsTreeSet());
Hope will above work for you. 希望将为您工作。 Please let me know if it didnt work 如果它不起作用,请告诉我
check below sample 检查以下样本
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.