In this program it is not identifying the compartor
, I don't want to use the 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
From the documentation :
A NavigableSet implementation based on a TreeMap. 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. But in this line
treeSet = build.sendAsTreeSet();
you overwrite the treeSet
you just created in the previous line. That tree set most likely doesn't have a Comparator
set.
There are two ways to solve this:
build
has a Comparator
set (based on your comment, use new TreeSet(new Compared())
in that method). build
implement Comparable
. Since you use
TreeSet treeSet = new TreeSet();
in your sendAsTreeSet
method, the treeset tries to sort the elements added using theire natural ordering . Witch impiles the class of your objects musst implement the Compareable interface.
You should replace that line with
TreeSet treeSet = new TreeSet( new Compared());
The same line in the main method can be removed.
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.
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.
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
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.