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:
- Why only one elemnt is being shown
- How to get the names in ascending order