Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

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()

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

Open in new window



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

Open in new window


please advise
ASKER CERTIFIED SOLUTION
Avatar of Am P
Am P
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

Well. it depends on the writer of custom class and requirement of compareTo().

i read it is responsibility of writer but since this is example author did not bother to write in this case?
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