Example
package com.w3schools;
import java.util.LinkedHashMap;
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[]){
LinkedHashMap<Employee, String> linkedHashMap = new LinkedHashMap<Employee, String>();
linkedHashMap.put(new Employee(1,"Jai",50000), "JAI");
linkedHashMap.put(new Employee(2,"Mahesh",80000), "MAHSESH");
linkedHashMap.put(new Employee(3,"Vishal",60000), "VISHAL");
linkedHashMap.put(new Employee(4,"Hemant",64000), "HEMANT");
System.out.println("HashMap elements:" + linkedHashMap);
System.out.println("Add duplicate element.");
linkedHashMap.put(new Employee(4,"Hemant",64000), "HEMANT");
System.out.println("HashMap elements:" + linkedHashMap);
}
} |
package com.w3schools;
import java.util.LinkedHashMap;
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[]){
LinkedHashMap<Employee, String> linkedHashMap = new LinkedHashMap<Employee, String>();
linkedHashMap.put(new Employee(1,"Jai",50000), "JAI");
linkedHashMap.put(new Employee(2,"Mahesh",80000), "MAHSESH");
linkedHashMap.put(new Employee(3,"Vishal",60000), "VISHAL");
linkedHashMap.put(new Employee(4,"Hemant",64000), "HEMANT");
System.out.println("HashMap elements:" + linkedHashMap);
System.out.println("Add duplicate element.");
linkedHashMap.put(new Employee(4,"Hemant",64000), "HEMANT");
System.out.println("HashMap elements:" + linkedHashMap);
}
}
Output
In hashcode
In hashcode
In hashcode
In hashcode
HashMap elements:{Id: 1, Name: Jai, Salary: 50000=JAI, Id: 2, Name: Mahesh,
Salary: 80000=MAHSESH, Id: 3, Name: Vishal, Salary: 60000=VISHAL, Id: 4, Name: Hemant, Salary: 64000=HEMANT}
Add duplicate element.
In hashcode
In equals
HashMap elements:{Id: 1, Name: Jai, Salary: 50000=JAI, Id: 2, Name: Mahesh,
Salary: 80000=MAHSESH, Id: 3, Name: Vishal, Salary: 60000=VISHAL, Id: 4, Name: Hemant, Salary: 64000=HEMANT} |
In hashcode
In hashcode
In hashcode
In hashcode
HashMap elements:{Id: 1, Name: Jai, Salary: 50000=JAI, Id: 2, Name: Mahesh,
Salary: 80000=MAHSESH, Id: 3, Name: Vishal, Salary: 60000=VISHAL, Id: 4, Name: Hemant, Salary: 64000=HEMANT}
Add duplicate element.
In hashcode
In equals
HashMap elements:{Id: 1, Name: Jai, Salary: 50000=JAI, Id: 2, Name: Mahesh,
Salary: 80000=MAHSESH, Id: 3, Name: Vishal, Salary: 60000=VISHAL, Id: 4, Name: Hemant, Salary: 64000=HEMANT}