0
import java.text.ParseException;
import java.util.Iterator;
import java.util.TreeSet;
public class Test {
    public static void main(String[] args) throws ParseException {
        TreeSet<Employee> ts = new TreeSet<Employee>();
        Employee emp1 = new Employee();
        Employee emp2 = new Employee();
         emp1.setName("CDD");
         emp2.setName("BCC");
         ts.add(emp1);
         ts.add(emp2);
         Iterator<Employee> itr=ts.iterator();
            while(itr.hasNext()){
                Employee c=itr.next();
                System.out.println(c.getName());
            }
    }
}

public class Employee implements Comparable<Employee> 
{   
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int compareTo(Employee arg0) {
        // TODO Auto-generated method stub
        return 0;
    }



}

I am adding custom Employee Objects to the tree set to get the names in the ascending order.

I have this program.

Could you please let me know:

  1. Why only one elemnt is being shown
  2. How to get the names in ascending order
Rob Audenaerde
  • 19,195
  • 10
  • 76
  • 121

1 Answers1

3

You need to create a compareTo method that won't compare all Employees equal, which is what your compareTo method is doing by always returning 0. When a TreeSet (or any Set) finds an object that is "equal" to something already in the set, it won't add it.

You can compare Employees by comparing their names.

@Override
public int compareTo(Employee arg0) {
    return this.name.compareTo(arg0.name);
}

Or you can create your own implementation of compareTo, returning a negative number when this object is "less than" the object argument, 0 when this object is "equal to" the object argument, and a positive number when this object is "greater than" the object argument, per the compareTo contract.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • Interessting, that the JavaDoc of `TreeSet.add` is wrong here, because it says "More formally, adds the specified element e to this set if the set contains no element e2 such that `(e==null ? e2==null : e.equals(e2))`" and that returns `false`. Should be added that `compareTo` counts if the passed type implements the `Comparable` interface. – Tom Aug 06 '15 at 19:20
  • @Tom In general `Set`s do use `equals`, but `TreeSet` breaks from this by using `Comparable` objects or a `Comparator`. But its [Javadocs do state](https://docs.oracle.com/javase/8/docs/api/java/util/TreeSet.html): "Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface.". To be consistent with equals, @Kiran would also need to define an `equals` method that compares the employee names. – rgettman Aug 06 '15 at 19:24
  • Thanks , i have got my equals method as @Override public boolean equals (Employee other) { if (!(other instanceof Employee)) return false; Listed ob = (Listed) other; return name.equals(ob.name) ; } –  Aug 06 '15 at 19:30
  • @rgettman It needs to be consistent for using other methods than `add`, since this methods relies solely on `compareTo`. – Tom Aug 06 '15 at 19:35
  • @Kiran You need to override `equals` and `hashCode`. – Tom Aug 06 '15 at 19:36
  • Thanks , hashcode is @Override public int hashCode () { return Arrays.hashCode(new String[]{name}); } –  Aug 06 '15 at 19:37
  • please let me know if thats fine –  Aug 06 '15 at 19:38
  • If you're using an IDE like Eclipse, then let it generate these methods for you. For example your equals method `Listed ob = (Listed) other;` .. what is `Listed`? – Tom Aug 06 '15 at 19:51