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
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Amitkumar PSr. ConsultantCommented:
Well. it depends on the writer of custom class and requirement of compareTo().

Even to provide default implementation, how does java identify which field(s) of a custom class to use for custom sorting order.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gurpsbassiCommented:
Comparable is an interface.
You are not forced to implement it when you create a new class.

It entirely depends on your requirement. The jdk cant generate a default implementatiom for you.
CEHJCommented:
I wonder why employee class has no default sorting order implementation(not custom one) with method compareTo()
Because the example is for Comparator, not Comparable

See the Javadoc for differences and similarities between the two
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

gudii9Author Commented:
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?
Amitkumar PSr. ConsultantCommented:
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.
gurpsbassiCommented:
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?
CEHJCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.