
Hi
using hashcode() and equal() method and hashmap object how to compare employee lastname and firstname ,display in console

Here is an example that uses hashcode() and equal() method and store the employee information into hashmap and display it.
import java.util.*;
public class Employee {
private String firstname;
private String lastname;
public Employee(String firstname, String lastname){
this.firstname = firstname;
this.lastname = lastname;
}
public String getFirstname() {
return firstname;
}
public String getLastname() {
return lastname;
}
public String toString() {
return lastname + ", " + firstname;
}
public int hashCode() {
return this.toString().hashCode();
}
public boolean equals(Object emp) {
if ( emp == null ) return false;
if ( this.getClass() != emp.getClass() ) return false;
String name = ((Employee)emp).toString();
return this.toString().equals(name);
}
public static void main(String[]args){
HashMap<Integer,Employee> map = new HashMap<Integer,Employee>();
map.put(1,new Employee("Annie","Smith"));
map.put(2,new Employee("Angel","De'souza"));
map.put(3,new Employee("John","Paul"));
map.put(4,new Employee("Joe","Smith"));
for (Employee employee : map.values())
{
System.out.println(employee.toString());
}
}
}