fork download
  1. import java.util.*;
  2.  
  3. class Scratch {
  4. public static void main(String[] args) {
  5.  
  6. final Set<Employee> eSet = new TreeSet<>();
  7.  
  8. eSet.add(new Employee(1, "john", 20000));
  9. eSet.add(new Employee(2, "jim", 10000));
  10. eSet.add(new Employee(9, "mike", 50000));
  11. eSet.add(new Employee(3, "jack", 30000));
  12. eSet.add(new Employee(3, "david", 40000));
  13. eSet.add(new Employee(9, "liam", 80000));
  14. eSet.add(new Employee(9, "brad", 89000));
  15. eSet.add(new Employee(3, "jason", 85000));
  16. eSet.add(new Employee(2, "ted", 35000));
  17.  
  18. for(Employee e: eSet) {
  19. System.out.println(e);
  20. }
  21. }
  22. }
  23.  
  24. class Employee implements Comparable<Employee> {
  25. int empId;
  26. String empName;
  27. double salary;
  28.  
  29. public Employee(int empId, String empName, double salary) {
  30. this.empId = empId;
  31. this.empName = empName;
  32. this.salary = salary;
  33. }
  34.  
  35. @Override
  36. public int hashCode() {
  37. return empId;
  38. }
  39.  
  40. @Override
  41. public boolean equals(Object o) {
  42. if(this == o) return true;
  43. if(o == null || this.getClass() != o.getClass()) return false;
  44.  
  45. Employee e = (Employee) o;
  46. return (this.empId == e.empId);
  47. }
  48.  
  49. @Override
  50. public String toString() {
  51. return empId + " " + empName + " " + salary;
  52. }
  53.  
  54. @Override
  55. public int compareTo(Employee e) {
  56. return Integer.compare(empId, e.empId);
  57. }
  58. }
Success #stdin #stdout 0.18s 55640KB
stdin
Standard input is empty
stdout
1 john 20000.0
2 jim 10000.0
3 jack 30000.0
9 mike 50000.0