Master exception handling and error management in Java applications.
Hashing is a fundamental concept in computer science that allows for efficient data retrieval. In Java, proper implementation of hashCode() is essential for the correct functioning of hash-based collections like HashMap, HashSet, and Hashtable.
public class Person {
private final String firstName;
private final String lastName;
private final int age;
public Person(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
Objects.equals(firstName, person.firstName) &&
Objects.equals(lastName, person.lastName);
}
@Override
public int hashCode() {
return Objects.hash(firstName, lastName, age);
}
}
public class Employee extends Person {
private final String employeeId;
public Employee(String firstName, String lastName, int age, String employeeId) {
super(firstName, lastName, age);
this.employeeId = employeeId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Employee employee = (Employee) o;
return Objects.equals(employeeId, employee.employeeId);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), employeeId);
}
}