Guest User

Untitled

a guest
Dec 23rd, 2017
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. package collection;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Set;
  7.  
  8. public class HashCodeEqualsDemo {
  9.  
  10. public static void main(String[] args) {
  11.  
  12.  
  13. // Add method of set will first check for hashcode, if method is not found, it
  14. // will create a new Object even if equals has been overridden
  15. // If hashcode has been overridden, and two employees have same id (thus same hashcode
  16. // as per our implementation), it will execute equals method, to find if two objects are equal.
  17. // For set to work properly for a custom reference type, it is mandatory to override both hashcode and equals
  18. Set<Employee> employeeSet = new HashSet<>();
  19. employeeSet.add(new Employee("ABC", "abc@gmail.com", 123));
  20. employeeSet.add(new Employee("ABC", "abc@gmail.com", 123));
  21. employeeSet.add(new Employee("XYZ", "abc@gmail.com", 100));
  22. employeeSet.add(new Employee("XYZ", "xyz@gmail.com", 123));
  23. System.out.println(employeeSet.size()); // 3
  24.  
  25.  
  26. //Add method of list will just check the equality using equals method. Following will work even if
  27. // hashcode has not been overridden.
  28.  
  29. List<Employee> employeeList = new ArrayList<>();
  30. employeeList.add(new Employee("ABC", "abc@gmail.com", 123));
  31. employeeList.add(new Employee("ABC", "abc@gmail.com", 123));
  32. employeeList.add(new Employee("XYZ", "abc@gmail.com", 100));
  33. employeeList.add(new Employee("XYZ", "xyz@gmail.com", 123));
  34. System.out.println(employeeList.contains(new Employee("ABC", "abc@gmail.com", 123))); // true
  35. System.out.println(employeeList.contains(new Employee("PQR", "pqr@gmail.com", 500))); // false
  36. System.out.println(employeeList.contains(new Employee("PQR", "abc@gmail.com", 500))); // true
  37.  
  38. }
  39.  
  40. }
  41.  
  42.  
  43. class Employee {
  44.  
  45. private String name;
  46.  
  47. public int getId() {
  48. return id;
  49. }
  50.  
  51. public void setId(int id) {
  52. this.id = id;
  53. }
  54.  
  55. private int id;
  56.  
  57. public String getName() {
  58. return name;
  59. }
  60.  
  61. public void setName(String name) {
  62. this.name = name;
  63. }
  64.  
  65. public String getEmail() {
  66. return email;
  67. }
  68.  
  69. public void setEmail(String email) {
  70. this.email = email;
  71. }
  72.  
  73. private String email;
  74.  
  75. Employee(String name, String email, int id) {
  76. this.name = name;
  77. this.email = email;
  78. this.id = id;
  79. }
  80.  
  81. @Override
  82. public boolean equals(Object object) {
  83. System.out.println("Equals called");
  84. if (this == object) {
  85. return true;
  86. }
  87.  
  88. if (object == null || (this.getClass() != object.getClass()))
  89. return false;
  90.  
  91. Employee employee = (Employee) object;
  92. return this.email.equals(employee.email);
  93. }
  94.  
  95. @Override
  96. public int hashCode() {
  97. System.out.println("hashcode called");
  98. return this.id;
  99. }
  100. }
Add Comment
Please, Sign In to add comment