1

I need to add objects to the Binary search tree and want to write my own compareTo method. How should i go about this. Actually i am trying to implement a custom TreeSet. I am confused where to implement a comparator or comparable and define it. Any help appreciated

4 Answers 4

1

Your object must be implements interface Comparable

class MyClass implements Comparable { 
  int compareTo(){...}
}
final Set set = new TreeSet();
set.add(new MyClass());
....
set.add(new MyClass());

or make our own comparator:

class MyClass {}

class MyComparator implements Comparator {
    int compareTo(Object o1, Object o2){
      ...
    }
}
final Set set = new TreeSet(new MyComparator());
set.add(new MyClass());
....
set.add(new MyClass());
Sign up to request clarification or add additional context in comments.

Comments

1

You want to make sure the objects you add to the TreeSet implement Comparable. This means making sure the stored objects implement a compareTo method as defined in the Javadoc.

You probably don't need a Comparator.

By the way, we recently created a tutorial on exactly this topic. Hopefully it can help you.

Comments

1

Please refer below code

package com.example.treeset;

    import java.util.Comparator;
    import java.util.TreeSet;

    public class MyCompUser {

        public static void main(String a[]){
            //By using name comparator (String comparison)
            TreeSet<Empl> nameComp = new TreeSet<Empl>(new MyNameComp());
            nameComp.add(new Empl("Ram",3000));
            nameComp.add(new Empl("John",6000));
            nameComp.add(new Empl("Crish",2000));
            nameComp.add(new Empl("Tom",2400));
            for(Empl e:nameComp){
                System.out.println(e);
            }
            System.out.println("===========================");
            //By using salary comparator (int comparison)
            TreeSet<Empl> salComp = new TreeSet<Empl>(new MySalaryComp());
            salComp.add(new Empl("Ram",3000));
            salComp.add(new Empl("John",6000));
            salComp.add(new Empl("Crish",2000));
            salComp.add(new Empl("Tom",2400));
            for(Empl e:salComp){
                System.out.println(e);
            }
        }
    }

    class MyNameComp implements Comparator<Empl>{

        @Override
        public int compare(Empl e1, Empl e2) {
            return e1.getName().compareTo(e2.getName());
        }
    }   

    class MySalaryComp implements Comparator<Empl>{

        @Override
        public int compare(Empl e1, Empl e2) {
            if(e1.getSalary() > e2.getSalary()){
                return 1;
            } else {
                return -1;
            }
        }
    }

    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;
        }
    }

Comments

1

For a TreeSet, you need to give your own implementation of the Comparator interface.

Something like this, maybe?

// Using an anonymous interface
Set<Foobar> example = new TreeSet<Foobar>(new Comparator<Foobar>() {
  @Override
  public int compare(Foobar f1, Foobar f2) {
    // How will you compare f1 and f2??
    return 0;
  }

  @Override
  public boolean equals(Object obj) {
    // How will you determine if objects are equal?
    return false;
  }
}
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.