gudii9
asked on
TreeSet comparator example
Hi,
I am looking at below example
http://java2novice.com/java-collections-and-util/treeset/comparator-object/
I wonder why employee class has no default sorting order implementation(not custom one) with method compareTo()
I have expecte above code as below
please advise
I am looking at below example
http://java2novice.com/java-collections-and-util/treeset/comparator-object/
I wonder why employee class has no default sorting order implementation(not custom one) with method compareTo()
class Empl{
private String name;
private int salary;
public Empl(String n, int s){
this.name = n;
this.salary = s;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String toString(){
return "Name: "+this.name+"-- Salary: "+this.salary;
}
}
- See more at: http://java2novice.com/java-collections-and-util/treeset/comparator-object/#sthash.BHVK2GLy.dpuf
I have expecte above code as below
class Empl implements comparable{
private String name;
private int salary;
public Empl(String n, int s){
this.name = n;
this.salary = s;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String toString(){
return "Name: "+this.name+"-- Salary: "+this.salary;
}
public int compareTo(){
//default sorting using some ID etc unless user want custom sorting on first name et then they create classes like //MySalaryComp
}
}
- See more at: http://java2novice.com/java-collections-and-util/treeset/comparator-object/#sthash.BHVK2GLy.dpuf
please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Correct. Generally examples are stick to the specific topic, here it is Comparator. Examples explain what to use when and how. It doesn't contain multiple things together until require.
i read it is responsibility of writer but since this is example author did not bother to write in this case?
The author didn't use comparable because he is trying to teach you about comparators. Do you understand?
The author didn't use comparable because he is trying to teach you about comparators. Do you understand?
At least one person here doesn't
ASKER
i read it is responsibility of writer but since this is example author did not bother to write in this case?