Example:
package com.w3schools;
import java.util.Hashtable;
class Employee{
private String name;
private int salary;
private int id;
public Employee(int id, String name, int salary){
this.id = id;
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String toString(){
return "Id: "+this.id+", Name: "+this.name+", Salary: "+this.salary;
}
@Override
public int hashCode() {
System.out.println("In hashcode");
return this.getId();
}
@Override
public boolean equals(Object obj) {
Employee e = null;
if(obj instanceof Employee){
e = (Employee) obj;
}
System.out.println("In equals");
if(this.getId() == e.getId()){
return true;
} else {
return false;
}
}
}
public class Test {
public static void main(String args[]){
Hashtable<Employee,String> hashtable = new Hashtable<Employee, String>();
hashtable.put(new Employee(1,"Jai",50000), "JAI");
hashtable.put(new Employee(2,"Mahesh",80000), "MAHESH");
hashtable.put(new Employee(3,"Vishal",60000), "VISHAL");
hashtable.put(new Employee(4,"Hemant",64000), "HEMANT");
System.out.println("Fecthing value by creating new key:");
Employee e = new Employee(3,"Vikas",54000);
System.out.println("Newly created element: " + e);
System.out.println("Element with new key - "+hashtable.get(e));
}
} |
package com.w3schools;
import java.util.Hashtable;
class Employee{
private String name;
private int salary;
private int id;
public Employee(int id, String name, int salary){
this.id = id;
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String toString(){
return "Id: "+this.id+", Name: "+this.name+", Salary: "+this.salary;
}
@Override
public int hashCode() {
System.out.println("In hashcode");
return this.getId();
}
@Override
public boolean equals(Object obj) {
Employee e = null;
if(obj instanceof Employee){
e = (Employee) obj;
}
System.out.println("In equals");
if(this.getId() == e.getId()){
return true;
} else {
return false;
}
}
}
public class Test {
public static void main(String args[]){
Hashtable<Employee,String> hashtable = new Hashtable<Employee, String>();
hashtable.put(new Employee(1,"Jai",50000), "JAI");
hashtable.put(new Employee(2,"Mahesh",80000), "MAHESH");
hashtable.put(new Employee(3,"Vishal",60000), "VISHAL");
hashtable.put(new Employee(4,"Hemant",64000), "HEMANT");
System.out.println("Fecthing value by creating new key:");
Employee e = new Employee(3,"Vikas",54000);
System.out.println("Newly created element: " + e);
System.out.println("Element with new key - "+hashtable.get(e));
}
}
Output
In hashcode
In hashcode
In hashcode
In hashcode
Fecthing value by creating new key:
Newly created element: Id: 3, Name: Vikas, Salary: 54000
In hashcode
In equals
Element with new key - VISHAL |
In hashcode
In hashcode
In hashcode
In hashcode
Fecthing value by creating new key:
Newly created element: Id: 3, Name: Vikas, Salary: 54000
In hashcode
In equals
Element with new key - VISHAL