fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.ArrayList;
  7. import java.util.HashSet;
  8. import java.util.List;
  9. import java.util.Set;
  10.  
  11. /* Name of the class has to be "Main" only if the class is public. */
  12. class Ideone
  13. {
  14. public static void main(String[] args) {
  15. List<Employee> list = new ArrayList<>();
  16. list.add(new Employee(123, "zMadhu", "1000$", 1));
  17. list.add(new Employee(332, "bSudhan", "2000$", 2));
  18. list.add(new Employee(54, "cKongarass", "3000$", 3));
  19. list.add(new Employee(54, "xKongarass", "3000$", 4));
  20. list.add(new Employee(54, "aKongarass", "3000$", 5));
  21.  
  22. Set<Employee> em = new HashSet<>(list);
  23. System.out.println(em);
  24.  
  25. }
  26. }
  27.  
  28. class Employee {
  29.  
  30. private int id;
  31. private String name;
  32. private String salary;
  33. private String recordStatus;
  34. private int key;
  35.  
  36. public Employee(int id, String name, String salary, int key) {
  37. super();
  38. this.id = id;
  39. this.name = name;
  40. this.salary = salary;
  41. this.key = key;
  42. }
  43.  
  44. @Override
  45. public boolean equals(Object o) {
  46. if (this == o) return true;
  47. if (!(o instanceof Employee)) return false;
  48.  
  49. Employee employee = (Employee) o;
  50.  
  51. return id == employee.id;
  52. }
  53.  
  54. @Override
  55. public int hashCode() {
  56. return id;
  57. }
  58.  
  59. @Override
  60. public String toString() {
  61. return id + "";
  62. }
  63. }
Success #stdin #stdout 0.05s 2184192KB
stdin
Standard input is empty
stdout
[54, 123, 332]